[3910] | 1 | import subprocess |
---|
| 2 | |
---|
| 3 | def MakeByte2str(arg): |
---|
| 4 | '''Convert output from subprocess pipes (bytes) to str (unicode) in Python 3. |
---|
| 5 | In Python 2: Leaves output alone (already str). |
---|
| 6 | Leaves stuff of other types alone (including unicode in Py2) |
---|
| 7 | Works recursively for string-like stuff in nested loops and tuples. |
---|
| 8 | |
---|
| 9 | typical use:: |
---|
| 10 | out = MakeByte2str(out) |
---|
| 11 | or:: |
---|
| 12 | out,err = MakeByte2str(s.communicate()) |
---|
| 13 | |
---|
| 14 | ''' |
---|
| 15 | if isinstance(arg,str): return arg |
---|
| 16 | if isinstance(arg,bytes): return arg.decode() |
---|
| 17 | if isinstance(arg,list): |
---|
| 18 | return [MakeByte2str(i) for i in arg] |
---|
| 19 | if isinstance(arg,tuple): |
---|
| 20 | return tuple([MakeByte2str(i) for i in arg]) |
---|
| 21 | return arg |
---|
| 22 | svn = 'svn' # assume in path |
---|
| 23 | cmd = [svn,'info','https://subversion.xray.aps.anl.gov/pyGSAS/trunk'] |
---|
| 24 | s = subprocess.Popen(cmd, |
---|
| 25 | stdout=subprocess.PIPE,stderr=subprocess.PIPE) |
---|
| 26 | out,err = MakeByte2str(s.communicate()) |
---|
| 27 | if err: |
---|
| 28 | print ('subversion error!\nout=%s'%out) |
---|
| 29 | print ('err=%s'%err) |
---|
| 30 | sys.exit(1) |
---|
| 31 | for line in out.split('\n'): |
---|
| 32 | if line.startswith('Rev'): |
---|
| 33 | version = line.split()[1].strip() |
---|
| 34 | break |
---|
| 35 | else: |
---|
| 36 | print ('Version not found!\nout=%s'%out) |
---|
| 37 | sys.exit(1) |
---|
| 38 | for fil in 'g2complete/meta.yaml','g2full/construct.yaml': |
---|
| 39 | fp = open(fil+'.template','r') |
---|
| 40 | out = fp.read().replace('**Version**',version) |
---|
| 41 | fp.close() |
---|
[3912] | 42 | print('Creating',fil) |
---|
[3910] | 43 | fp = open(fil,'w') |
---|
| 44 | fp.write(out) |
---|
| 45 | fp.close() |
---|
| 46 | |
---|