def test_init(self): pipreqs.init({'<path>':self.project, '--savepath':None}) assert os.path.exists(self.requirements_path) == 1 with open(self.requirements_path, "r") as f: data = f.read() for item in self.modules[:-1]: self.assertTrue(item in data)
def test_init_local_only(self): pipreqs.init( {'<path>': self.project, '--savepath': None, '--use-local': True}) assert os.path.exists(self.requirements_path) == 1 with open(self.requirements_path, "r") as f: self.assertEqual( len(f.readlines()), 2, 'Only two local packages should be found')
def test_init(self): pipreqs.init({'<path>': self.project, '--savepath': None, '--use-local': None, '--force': True, '--proxy':None, '--pypi-server':None}) assert os.path.exists(self.requirements_path) == 1 with open(self.requirements_path, "r") as f: data = f.read().lower() for item in self.modules[:-2]: self.assertTrue(item.lower() in data)
def test_init_overwrite(self): with open(self.requirements_path, "w") as f: f.write("should_not_be_overwritten") pipreqs.init({'<path>': self.project, '--savepath': None, '--use-local': None, '--force': None}) assert os.path.exists(self.requirements_path) == 1 with open(self.requirements_path, "r") as f: data = f.read().lower() self.assertEqual(data, "should_not_be_overwritten")
def test_init_local_only(self): pipreqs.init({'<path>': self.project, '--savepath': None, '--use-local': True, '--force': True, '--proxy':None, '--pypi-server':None}) assert os.path.exists(self.requirements_path) == 1 with open(self.requirements_path, "r") as f: data = f.readlines() for item in data: item = item.strip().split(" == ") self.assertTrue(item[0].lower() in self.local)
def test_init_savepath(self): pipreqs.init({'<path>': self.project, '--savepath': self.alt_requirement_path, '--use-local': None}) assert os.path.exists(self.alt_requirement_path) == 1 with open(self.alt_requirement_path, "r") as f: data = f.read().lower() for item in self.modules[:-2]: self.assertTrue(item.lower() in data) for item in self.modules2: self.assertTrue(item.lower() in data)
def test_init(self): """ Test that all modules we will test upon, are in requirements file """ pipreqs.init({'<path>': self.project, '--savepath': None, '--print': False, '--use-local': None, '--force': True, '--proxy':None, '--pypi-server':None, '--diff': None, '--clean': None}) assert os.path.exists(self.requirements_path) == 1 with open(self.requirements_path, "r") as f: data = f.read().lower() for item in self.modules[:-3]: self.assertTrue(item.lower() in data)
def test_init_overwrite(self): """ Test that if requiremnts.txt exists, it will not automatically be overwritten """ with open(self.requirements_path, "w") as f: f.write("should_not_be_overwritten") pipreqs.init({'<path>': self.project, '--savepath': None, '--use-local': None, '--force': None, '--proxy':None, '--pypi-server':None}) assert os.path.exists(self.requirements_path) == 1 with open(self.requirements_path, "r") as f: data = f.read().lower() self.assertEqual(data, "should_not_be_overwritten")
def test_init_local_only(self): """ Test that items listed in requirements.text are the same as locals expected """ pipreqs.init({'<path>': self.project, '--savepath': None, '--print': False, '--use-local': True, '--force': True, '--proxy':None, '--pypi-server':None, '--diff': None, '--clean': None}) assert os.path.exists(self.requirements_path) == 1 with open(self.requirements_path, "r") as f: data = f.readlines() for item in data: item = item.strip().split("==") self.assertTrue(item[0].lower() in self.local)
def test_init_overwrite(self): with open(self.requirements_path, "w") as f: f.write("should_not_be_overwritten") pipreqs.init({ '<path>': self.project, '--savepath': None, '--use-local': None, '--force': None }) assert os.path.exists(self.requirements_path) == 1 with open(self.requirements_path, "r") as f: data = f.read().lower() self.assertEqual(data, "should_not_be_overwritten")
def test_init_savepath(self): """ Test that we can save requiremnts.tt correctly to a different path """ pipreqs.init({'<path>': self.project, '--savepath': self.alt_requirement_path, '--use-local': None, '--proxy':None, '--pypi-server':None}) assert os.path.exists(self.alt_requirement_path) == 1 with open(self.alt_requirement_path, "r") as f: data = f.read().lower() for item in self.modules[:-3]: self.assertTrue(item.lower() in data) for item in self.modules2: self.assertTrue(item.lower() in data)
def test_init_savepath(self): """ Test that we can save requiremnts.tt correctly to a different path """ pipreqs.init({'<path>': self.project, '--savepath': self.alt_requirement_path, '--use-local': None, '--proxy':None, '--pypi-server':None, '--print': False, "--diff": None, "--clean": None}) assert os.path.exists(self.alt_requirement_path) == 1 with open(self.alt_requirement_path, "r") as f: data = f.read().lower() for item in self.modules[:-3]: self.assertTrue(item.lower() in data) for item in self.modules2: self.assertTrue(item.lower() in data)
def test_init(self): """ Test that all modules we will test upon, are in requirements file """ pipreqs.init({'<path>': self.project, '--savepath': None, '--print': False, '--use-local': None, '--force': True, '--proxy':None, '--pypi-server':None, '--diff': None, '--clean': None}) assert os.path.exists(self.requirements_path) == 1 with open(self.requirements_path, "r") as f: data = f.read().lower() for item in self.modules[:-3]: self.assertTrue(item.lower() in data) # It should be sorted based on names. data = data.strip().split('\n') self.assertEqual(data, sorted(data))
def test_init_local_only(self): """ Test that items listed in requirements.text are the same as locals expected """ pipreqs.init( {'<path>': self.project, '--savepath': None, '--print': False, '--use-local': True, '--force': True, '--proxy': None, '--pypi-server': None, '--diff': None, '--clean': None} ) assert os.path.exists(self.requirements_path) == 1 with open(self.requirements_path, "r") as f: data = f.readlines() for item in data: item = item.strip().split("==") self.assertTrue(item[0].lower() in self.local)
def test_init_overwrite(self): """ Test that if requiremnts.txt exists, it will not be automatically overwritten """ with open(self.requirements_path, "w") as f: f.write("should_not_be_overwritten") pipreqs.init( {'<path>': self.project, '--savepath': None, '--use-local': None, '--force': None, '--proxy': None, '--pypi-server': None, '--print': False, '--diff': None, '--clean': None} ) assert os.path.exists(self.requirements_path) == 1 with open(self.requirements_path, "r") as f: data = f.read().lower() self.assertEqual(data, "should_not_be_overwritten")
def test_ignored_directory(self): """ Test --ignore parameter """ pipreqs.init( {'<path>': self.project_with_ignore_directory, '--savepath': None, '--use-local': None, '--force': True, '--proxy':None, '--pypi-server':None, '--ignore':'.ignored_dir,.ignore_second' } ) with open(os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r") as f: data = f.read().lower() for item in ['click', 'getpass']: self.assertFalse(item.lower() in data)
def test_omit_version(self): """ Test --no-pin parameter """ pipreqs.init( {'<path>': self.project_with_ignore_directory, '--savepath': None, '--print': False, '--use-local': None, '--force': True, '--proxy': None, '--pypi-server': None, '--diff': None, '--clean': None, '--no-pin': True} ) with open(os.path.join( self.project_with_ignore_directory, "requirements.txt"), "r" ) as f: data = f.read().lower() for item in ['beautifulsoup4==4.8.1', 'boto==2.49.0']: self.assertFalse(item.lower() in data)
def test_init(self): """ Test that all modules we will test upon, are in requirements file """ pipreqs.init({ '<path>': self.project, '--savepath': None, '--use-local': None, '--force': True, '--proxy': None, '--pypi-server': None }) assert os.path.exists(self.requirements_path) == 1 with open(self.requirements_path, "r") as f: data = f.read().lower() for item in self.modules[:-3]: self.assertTrue(item.lower() in data)
def main(): wd = op.dirname(__file__) dst = op.join(wd, "requirements.txt") try: os.remove(dst) except FileNotFoundError: pass # this is a copy of the pipreqs main fcn, but we pass our own argv to docopt args = docopt(__doc__, argv=[], version=__version__) init(args) unsorted = open(dst, "r").readlines() unsorted = [x for x in unsorted if not x.startswith("x2df")] unsorted.append("x2df @ git+https://github.com/mb-89/x2df\n") sortedlst = sorted(unsorted) open(dst, "w").write("".join(sortedlst))
def test_ignored_directory(self): """ Test --ignore parameter """ pipreqs.init( {'<path>': self.project_with_ignore_directory, '--savepath': None, '--print': False, '--use-local': None, '--force': True, '--proxy':None, '--pypi-server':None, '--ignore':'.ignored_dir,.ignore_second', '--diff': None, '--clean': None } ) with open(os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r") as f: data = f.read().lower() for item in ['click', 'getpass']: self.assertFalse(item.lower() in data)
def test_clean(self): """ Test --clean parameter """ pipreqs.init( {'<path>': self.project, '--savepath': None, '--print': False, '--use-local': None, '--force': True, '--proxy': None, '--pypi-server': None, '--diff': None, '--clean': None} ) assert os.path.exists(self.requirements_path) == 1 pipreqs.init( {'<path>': self.project, '--savepath': None, '--print': False, '--use-local': None, '--force': None, '--proxy': None, '--pypi-server': None, '--diff': None, '--clean': self.requirements_path, '--no-pin': True} ) with open(self.requirements_path, "r") as f: data = f.read().lower() for item in self.modules[:-3]: self.assertTrue(item.lower() in data)
def test_clean_with_imports_to_clean(self): """ Test --clean parameter when there are imports to clean """ cleaned_module = 'sqlalchemy' pipreqs.init( {'<path>': self.project, '--savepath': None, '--print': False, '--use-local': None, '--force': True, '--proxy': None, '--pypi-server': None, '--diff': None, '--clean': None} ) assert os.path.exists(self.requirements_path) == 1 modules_clean = [m for m in self.modules if m != cleaned_module] pipreqs.init( {'<path>': self.project_clean, '--savepath': None, '--print': False, '--use-local': None, '--force': None, '--proxy': None, '--pypi-server': None, '--diff': None, '--clean': self.requirements_path, '--no-pin': True} ) with open(self.requirements_path, "r") as f: data = f.read().lower() self.assertTrue(cleaned_module not in data)
def test_dynamic_version_no_pin_scheme(self): """ Test --mode=no-pin """ pipreqs.init({ '<path>': self.project_with_ignore_directory, '--savepath': None, '--print': False, '--use-local': None, '--force': True, '--proxy': None, '--pypi-server': None, '--diff': None, '--clean': None, '--mode': 'no-pin' }) with open( os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r") as f: data = f.read().lower() for item in ['beautifulsoup4', 'boto']: self.assertTrue(item.lower() in data)
def test_dynamic_version_compat_scheme(self): """ Test --mode=compat """ pipreqs.init({ '<path>': self.project_with_ignore_directory, '--savepath': None, '--print': False, '--use-local': None, '--force': True, '--proxy': None, '--pypi-server': None, '--diff': None, '--clean': None, '--mode': 'compat' }) with open( os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r") as f: data = f.readlines() for item in data: symbol = '~=' message = 'symbol is not in item' self.assertIn(symbol, item, message)
def pack(ResultFolder): import pchain import sys import os if os.path.exists('packageinfo.json') == False: print('file packageinfo.json" not existed') return False f = None if pchain.ispython2 == True: f = open('packageinfo.json') else: f = open('packageinfo.json', encoding='utf-8') packagetxt = f.read() f.close() Service = pchain.cleinit() SrvGroup = Service._ServiceGroup packageinfo = SrvGroup._NewParaPkg() if packageinfo._FromJSon(packagetxt) == False: SrvGroup._ClearService() libstarpy._ModuleExit() print('load packageinfo.json" failed') return False PackageLang = packageinfo["PackageLang"] if PackageLang != "python" and PackageLang != "python2" and PackageLang != "python3" and PackageLang != "c" and PackageLang != "c#" and PackageLang != "java": print('PackageLang ' + PackageLang + ' is not defined or not supported') return False if PackageLang == "python" or PackageLang == "python2" or PackageLang == "python3": try: print( 'try to create requirements.txt for python dependence, using \"pipreqs\"' ) from pipreqs import pipreqs pipreqs.init({ '<path>': './', '--pypi-server': None, '--proxy': None, '--use-local': None, '--use-local': True, '--savepath': './requirements.txt', '--diff': None, '--clean': None, '--print': False }) if os.path.exists("requirements.txt"): if os.path.getsize("requirements.txt") > 3: print('create requirements.txt finish') else: os.remove("requirements.txt") print('no python dependence is needed') except Exception as exc: print("failed, requirements.txt need to be created manually") PackageEntry = packageinfo["PackageEntry"] if PackageEntry == None or len(PackageEntry) == 0: print('PackageEntry is not defined') return False print("begin update packageinfo.json ...") pchain.cleinit() #--save all object existedObjects = Service._AllObject() if PackageLang == "c": Result = Service._DoFile("", PackageEntry, "") else: if PackageLang == "python" or PackageLang == "python2" or PackageLang == "python3": import sys sys.path.insert(0, os.getcwd()) try: __import__(packageinfo['PackageName']) Result = [True, ''] except Exception as exc: import traceback traceback.print_exc() return False else: Result = Service._DoFile(PackageLang, PackageEntry, "") if Result[0] == False: print(Result[1]) return False createdObjects = Service._AllObject() def ObjectExist(which, obj): for val in obj._Iterator(): if val == which: return True return False newObjects = [] for val in createdObjects._Iterator(): if ObjectExist(val,existedObjects) == False and \ ( Service.PCDataBase._IsInst(val) == True or Service.PCProcBase._IsInst(val) == True ) : o_name = val._Name.split('.') if len(o_name) == 1: pass elif len(o_name) == 2: if o_name[0] == packageinfo["PackageName"]: pass else: print('failed, object ' + val._Name + ' is in the package namespace') return False else: print('failed, object ' + val._Name + ' is in the package namespace') return False newObjects.append(val) print("new object captured : ", o_name[len(o_name) - 1]) ObjectList = SrvGroup._NewParaPkg() for i in range(len(newObjects)): o_name = newObjects[i]._Name.split('.') ObjectList[ObjectList._Number] = packageinfo[ "PackageName"] + "." + o_name[len(o_name) - 1] packageinfo["ObjectList"] = ObjectList #Calculate md5 value without file packageinfo.json packageinfo["signature"] = getmd5() f = None if pchain.ispython2 == True: f = open('packageinfo.json', 'w') else: f = open('packageinfo.json', 'w', encoding='utf-8') packagetxt = f.write(packageinfo._ToJSon()) f.close() print("end update packageinfo.json ...") ZipPackageName = packageinfo["PackageName"] + "-" + packageinfo[ "PackageLang"].lower() + "." + packageinfo["PackageVersion"] pchain.cleterm() print("start packing ...") import zipfile def zippackag(ZipPackageName): file_news = ZipPackageName + '.zip' z = zipfile.ZipFile(file_news, 'w', zipfile.ZIP_DEFLATED) for dirpath, dirnames, filenames in os.walk("."): fpath = dirpath.replace(".", '') fpath = fpath and fpath + os.sep or '' for filename in filenames: if filename == file_news: pass else: z.write(os.path.join(dirpath, filename), fpath + filename) z.close() zippackag(ResultFolder + os.sep + ZipPackageName) print("finish ...") return True