Skip to content

Commit

Permalink
new zip file script and python3 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dkratzert committed Jan 27, 2018
1 parent 0881509 commit 9e8f6a1
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 11 deletions.
4 changes: 3 additions & 1 deletion dbfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,9 @@ def __iter__(self):
>>> [x for x in db][:3]
['napht', 'plusminus', 'fluorbenz']
"""
return iter(self.databases.keys())
# return iter(self.databases.keys()) #python2
return iter(list(self.databases.keys()))


def list_fragments(self):
# type: () -> list
Expand Down
8 changes: 4 additions & 4 deletions elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def nominalmass(self):
"""Return mass number of most abundant natural stable isotope."""
nominalmass = 0
maxabundance = 0
for massnum, iso in self.isotopes.items():
for massnum, iso in list(self.isotopes.items()):
if iso.abundance > maxabundance:
maxabundance = iso.abundance
nominalmass = massnum
Expand All @@ -224,7 +224,7 @@ def neutrons(self):
@lazyattr
def exactmass(self):
"""Return relative atomic mass calculated from isotopic composition."""
return sum(iso.mass * iso.abundance for iso in self.isotopes.values())
return sum(iso.mass * iso.abundance for iso in list(self.isotopes.values()))

@lazyattr
def eleconfig_dict(self):
Expand All @@ -241,7 +241,7 @@ def eleconfig_dict(self):
def eleshells(self):
"""Return number of electrons in shell as tuple."""
eleshells = [0, 0, 0, 0, 0, 0, 0]
for key, val in self.eleconfig_dict.items():
for key, val in list(self.eleconfig_dict.items()):
eleshells[key[0] - 1] += val
return tuple(ele for ele in eleshells if ele)

Expand All @@ -266,7 +266,7 @@ def validate(self):

mass = 0.0
frac = 0.0
for iso in self.isotopes.values():
for iso in list(self.isotopes.values()):
mass += iso.abundance * iso.mass
frac += iso.abundance
if abs(mass - self.mass) > 0.03:
Expand Down
36 changes: 36 additions & 0 deletions misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,42 @@ def check_file_exist(filename):
return status


def walkdir(rootdir, include="", exclude=""):
"""
Returns a list of files in all subdirectories with full path.
:param rootdir: base path from which walk should start
:param filter: list of file endings to include only e.g. ['.py', '.res']
:return: list of files
>>> walkdir("./setup/debian-package") #doctest: +REPORT_NDIFF +NORMALIZE_WHITESPACE +ELLIPSIS
['./setup/debian-package/.DS_Store', './setup/debian-package/howto.txt',
'./setup/debian-package/DEBIAN/postinst', './setup/debian-package/DEBIAN/control']
>>> walkdir("./modpath.iss")
['./modpath.iss']
>>> walkdir("./modpath.iss", exclude=['.iss'])
[]
>>> walkdir("./setup/debian-package", exclude=['.txt']) #doctest: +REPORT_NDIFF +NORMALIZE_WHITESPACE +ELLIPSIS
['./setup/debian-package/.DS_Store',
'./setup/debian-package/DEBIAN/postinst', './setup/debian-package/DEBIAN/control']
"""
results = []
if not os.path.isdir(rootdir):
if os.path.splitext(rootdir)[1] in exclude:
return []
return [rootdir]
for root, subFolders, files in os.walk(rootdir):
for file in files:
fullfilepath = os.path.join(root, file)
if exclude:
if os.path.splitext(fullfilepath)[1] in exclude:
continue
if include:
if os.path.splitext(fullfilepath)[1] in include:
results.append(fullfilepath)
else:
results.append(fullfilepath)
return results

def pairwise(iterable):
"""
s -> (s0,s1), (s2,s3), (s4, s5), ...
Expand Down
9 changes: 4 additions & 5 deletions scripts/mac_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import misc
import selfupdate


try: # Python2:
# noinspection PyCompatibility
import urllib2
Expand All @@ -13,10 +12,10 @@
import urllib
http_error = IOError

version = "205" # define the .dmg release version
version = "207" # define the .dmg release version

volname = "DSR-"+version
inputfile = "/Users/daniel/Downloads/DSR-{}.tar.gz".format(version, version)
inputfile = "../setup/Output/DSR-{}.tar.gz".format(version, version)
dmgname = os.path.abspath("../setup/Output/{}.dmg".format(volname))
skeldmg = os.path.abspath("../setup/Output/DSR-skel-rw.dmg")
finaltmpdmg = os.path.abspath("../setup/Output/DSR-tmp-rw.dmg")
Expand All @@ -33,7 +32,7 @@
subprocess.Popen(unmountcommmand, stderr=subprocess.PIPE)

print('Mounting .dmg file for mac deployment')
print(finaltmpdmg+"\n")
print('Mounting: ', finaltmpdmg, '\n')
subprocess.call(mountcommmand)

"""
Expand All @@ -52,7 +51,7 @@
# use instead of webserver stuff above
tmpdir = tempfile.mkdtemp() # a temporary directory
misc.extract_tarfile(inputfile, tmpdir)
selfupdate.move_dir(tmpdir+"/DSR-{}".format(version), '/Volumes/DSR-install/DSR')
selfupdate.move_dir(os.path.join(tmpdir, "DSR-{}".format(version)), '/Volumes/DSR-install/DSR/')
# end

#########################################################
Expand Down
75 changes: 75 additions & 0 deletions setup/make_zipfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""
Creates a zip file with the content of the StructureDB program.
"""
from tarfile import TarFile

import os

from dsr import VERSION
from misc import copy_file, remove_file, walkdir

version = VERSION

files = [
"afix.py",
"README",
"atomhandling.py",
"atoms.py",
"constants.py",
"dbfile.py",
"dsr.py",
"dsrparse.py",
"export.py",
"misc.py",
"elements.py",
"options.py",
"resfile.py",
"restraints.py",
"cf3fit.py",
"selfupdate.py",
"terminalsize.py",
"resi.py",
"refine.py",
"pyperclip.py",
"dsr_db.txt",
"manuals/DSR-manual.pdf",
"setup/dsr.sh",
"setup/dsr-mac",
"setup/dsr-linux",
"dsr",
"example/p21c.hkl",
"example/p21c.res",
"example/p21c_step0.res",
"example/p21c_step1.res",
"example/p21c_step2.ins",
"example/p21c_step3.res",
"example/p21c_final.res",
"example/p21n_cf3.hkl",
"example/p21n_cf3.res",
"networkx",
"mpmath"
]


def make_zip(filelist):
"""
:type filelist: list
"""
os.chdir('../')
zipfilen = 'setup/Output/DSR-{}.tar.gz'.format(version)
remove_file(zipfilen)
with TarFile(zipfilen, mode='w') as myzip:
for f in filelist:
print("Adding {}".format(f))
for file in walkdir(f, exclude=['.pyc']):
try:
myzip.add(file)
except FileNotFoundError as e:
print(e)
print("#####################")
return False
#copy_file(zipfilen, 'StructureFinder/scripts/Output/')
print("File written to {}".format(zipfilen))

if __name__ == "__main__":
make_zip(files)
2 changes: 1 addition & 1 deletion sql_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def get_picture(name):


def export_database():
for fid, fragment in enumerate(db.keys(), 1):
for fid, fragment in enumerate(list(db.keys()), 1):
Name = gl.get_fragment_name(fragment)
print("Exporting {}: {}: {}".format(fid, fragment, Name))
head = db[fragment]['restraints']
Expand Down

0 comments on commit 9e8f6a1

Please sign in to comment.