def _startup(self): self.debug("Using {} as startup config file".format(self.startup_config)) if not os.path.exists(self.startup_config): self.debug("{} doesn't exist. Creating it") sh.touch(self.startup_config) self._scrub_extra_dotfiles_block() self._add_extra_dotfiles_block()
def test_run(cli): with cli.isolated_filesystem(): sh.touch(dotenv_path) sh.cd(here) dotenv.set_key(dotenv_path, 'FOO', 'BAR') result = sh.dotenv('run', 'printenv', 'FOO').strip() assert result == 'BAR'
def execution_handler(*args, **kwargs): client = utils.Handler() client.rmdir(TEST_DIR) client.mkdir(TEST_DIR) sh.touch(TEST_FILE) func(*args, **kwargs) client.rmdir(TEST_DIR)
def test_console_script(cli): TEST_COMBINATIONS = ( # quote_mode, var_name, var_value, expected_result ("always", "HELLO", "WORLD", 'HELLO="WORLD"\n'), ("never", "HELLO", "WORLD", 'HELLO=WORLD\n'), ("auto", "HELLO", "WORLD", 'HELLO=WORLD\n'), ("auto", "HELLO", "HELLO WORLD", 'HELLO="HELLO WORLD"\n'), ) with cli.isolated_filesystem(): for quote_mode, variable, value, expected_result in TEST_COMBINATIONS: sh.touch(dotenv_path) sh.dotenv('-f', dotenv_path, '-q', quote_mode, 'set', variable, value) output = sh.cat(dotenv_path) assert output == expected_result sh.rm(dotenv_path) # should fail for not existing file result = cli.invoke(dotenv.cli.set, ['my_key', 'my_value']) assert result.exit_code != 0 # should fail for not existing file result = cli.invoke(dotenv.cli.get, ['my_key']) assert result.exit_code != 0 # should fail for not existing file result = cli.invoke(dotenv.cli.list, []) assert result.exit_code != 0
def commit_file_and_get_hash(repo_path, file_name): with sh.pushd(repo_path): sh.touch(file_name) sh.git("add", file_name) sh.git("commit", "-m", "'adding {}'".format(file_name)) commit_hash = str(sh.git("rev-parse", "HEAD")).strip() return commit_hash
def setup_log_dir(): homedir = os.path.expanduser('~') saved_ip_f = homedir + "/.ip_update/saved_ip" if not os.path.exists(saved_ip_f): sh.mkdir(homedir + "/.ip_update") sh.touch(saved_ip_f) return saved_ip_f
def main(logfile, cmts_directory, output_directory, username, password, network_code): """ Download Hinet waveforms based on cmtsolution files. """ # * set up logging logger.add(logfile, format="{time} {level} {message}", level="INFO") # * start the client client = init_client(username, password) logger.info("start to download.") # * here we get the cmtsolution files paths all_cmt_files = sorted(glob(join(cmts_directory, "*"))) # * mkdir for the data sh.mkdir("-p", output_directory) # * in the output directory, we use a file called local.filelist to select the cmt files to download sh.touch(join(output_directory, "local.filelist")) installed_cmt_files = np.loadtxt(join(output_directory, "local.filelist"), dtype=np.str) to_download_cmt_files = sorted( set(all_cmt_files) - set(installed_cmt_files)) data_list = [] ctable_list = [] for each_cmt_file in to_download_cmt_files: data, ctable = download_file(each_cmt_file, client, output_directory, network_code) if ((data != None) and (ctable != None)): data_list.append(data) ctable_list.append(ctable) with open(join(output_directory, "local.filelist"), 'a') as file: file.write(each_cmt_file + "\n") logger.success(f"download {basename(each_cmt_file)}") else: logger.error(f"skip {basename(each_cmt_file)}")
def _check_cow(image_dir): """ Check that the file system that stores guest images supports copy-on-write. """ try: src = f'{image_dir}/.cowcheck' dst = f'{image_dir}/.cowcheck1' sh.touch(src) sh.cp('--reflink=always', src, dst) return True except Exception: warn_msg = f""" Copy-on-write check failed. The file system where images are stored ({image_dir}) does not support copy-on-write. It is recommended to use an XFS or BTRFS file system with copy-on-write enabled as a storage location for S2E images, as this can save up to 60% of disk space. The building process checkpoints intermediate build steps with cp --reflink=auto to make use of copy-on-write if it is available. How to upgrade: 1. Create an XFS or BTRFS partition large enough to store the images that you need (~300 GB for all images). Make sure you use reflink=1 to enable copy-on-write when running mkfs.xfs. 2. Create a directory for guest images on that partition (e.g., /mnt/disk1/images) 3. Delete the "images" folder in your S2E environment 4. Create in your S2E environment a symbolic link called "images" to the directory you created in step 2 """ logger.warning(re.sub(r'^ {8}', '', warn_msg, flags=re.MULTILINE)) return False finally: sh.rm('-f', src) sh.rm('-f', dst)
def _create_simple_commit(self, message): """ Creates a simple commit with an empty test file. :param message: Commit message for the commit. """ test_filename = "test-file-" + str(uuid4()) touch(test_filename, _cwd=self.tmp_git_repo) git("add", test_filename, _cwd=self.tmp_git_repo) git("commit", "-m", message, _cwd=self.tmp_git_repo)
def main(data_directory, processes): """ extrct the data from the downloaded database. """ all_event_directories = sorted(glob(join(data_directory, "*"))) all_event_directories = [ item for item in all_event_directories if isdir(item) ] sh.touch(join(data_directory, "extract.filelist")) extreacted_event_directories = np.loadtxt(join(data_directory, "extract.filelist"), dtype=np.str) to_extract_event_directories = sorted( set(all_event_directories) - set(extreacted_event_directories)) for each_data_directory in to_extract_event_directories: ctables = glob(join(each_data_directory, "*ch")) datas = glob(join(each_data_directory, "*cnt")) if (len(ctables) == 0 or len(datas) == 0): continue ctable = ctables[0] data = datas[0] sac_path, pz_path = extract_sac(data, ctable, processes) # * to save files quota, we need to zip them and remove the sac and pz files. # tar sac event_path = dirname(sac_path) output_sac = join(event_path, "SAC.tar.gz") make_tarfile(output_sac, sac_path) output_pz = join(event_path, "PZ.tar.gz") make_tarfile(output_pz, pz_path) # remove sac and pz sh.rm("-rf", sac_path) sh.rm("-rf", pz_path) with open(join(data_directory, "extract.filelist"), "a") as file: file.write(each_data_directory + "\n")
def ensure_syncer_dir(): if path.isdir(syncer_dir): return username = input('GitHub username: '******'GitHub password: '******'clone', 'https://%s:%[email protected]/%s/%s.git' % (username, password, username, SYNCER_REPO_NAME), syncer_dir) needs_commit = False sh.cd(syncer_dir) if not path.isfile(path('manifest.json')): sh.touch('manifest.json') if not path.isdir(path('content')): sh.mkdir('content') if not path.isdir(path('backup')): sh.mkdir('backup') if not path.isfile(path('.gitignore')): needs_commit = True with open('.gitignore', 'w') as gitignore_file: gitignore_file.write('backup') if needs_commit: sh.git('add', '-A') sh.git('commit', '-m', 'Setting up scaffolding.')
def upload_script(name, data): full_path = conf.SCRIPTS_PATH + name sh.touch(full_path) shu.write_file(full_path, data) sh.chmod('u+x', full_path) return conf.mk_succ({'status': 'success'})
def _create_simple_commit(self, message, out=None, ok_code=None, env=None): """ Creates a simple commit with an empty test file. :param message: Commit message for the commit. """ # Let's make sure that we copy the environment in which this python code was executed as environment # variables can influence how git runs. # This was needed to fix https://github.com/jorisroovers/gitlint/issues/15 as we need to make sure to use # the PATH variable that contains the virtualenv's python binary. environment = os.environ if env: environment.update(env) test_filename = u"test-fïle-" + str(uuid4()) touch(test_filename, _cwd=self.tmp_git_repo) git("add", test_filename, _cwd=self.tmp_git_repo) # https://amoffat.github.io/sh/#interactive-callbacks if not ok_code: ok_code = [0] git("commit", "-m", message, _cwd=self.tmp_git_repo, _tty_in=True, _out=out, _ok_code=ok_code, _env=environment) return test_filename
def setupX11Authorization(xauthFile, display): try: sh.touch(xauthFile) except: print("touch failed") sh.xauth(sh.sed(sh.xauth("nlist", display), "-e", 's/^..../ffff/'), "-f", xauthFile, "nmerge", "-")
def test_doc(): with cd('../../doc'): project.build() touch('_build/.nojekyll') d = git('--no-pager', 'diff', '-w', '--', '_build') if d: print(d) raise ValueError('Diffs in website')
def test_default_path(tmp_path): sh.cd(str(tmp_path)) sh.touch(tmp_path / '.env') sh.dotenv('set', 'HELLO', 'WORLD') result = sh.dotenv('get', 'HELLO') assert result == 'HELLO=WORLD\n'
def setUpClass(cls): # Do back-up the existing corpus... assert not os.path.exists(TEST_LOCK_FILE) shutil.move(UC_HOME, UC_HOME_BACKUP) # Recreate the test directory. os.mkdir(UC_HOME) touch(TEST_LOCK_FILE)
def test__run__reboot_success(self): """ run: reboot success """ fname = "%s/../rebooting" % dirpath sh.touch(fname) self.bundle.run() self.assertEqual(False, os.path.isfile(fname))
def test_default_path(cli): with cli.isolated_filesystem(): sh.touch(dotenv_path) sh.cd(here) sh.dotenv('set', 'HELLO', 'WORLD') output = sh.dotenv('get', 'HELLO') assert output == 'HELLO="WORLD"\n' sh.rm(dotenv_path)
def setupX11Authorization(self): try: sh.touch(self.XAUTH) except: print("touch failed") sh.xauth( sh.sed(sh.xauth("nlist", self.DISPLAY), "-e", 's/^..../ffff/'), "-f", self.XAUTH, "nmerge", "-")
def test_default_path(cli): with cli.isolated_filesystem(): sh.touch(dotenv_path) sh.cd(here) sh.dotenv('set', 'HELLO', 'WORLD') output = sh.dotenv('get', 'HELLO') assert output == 'HELLO=WORLD\n' sh.rm(dotenv_path)
def create_playbook(books, play="site.yml"): for book in books: mkdir(book) mkdir(book + "/host_vars") mkdir(book + "/group_vars") mkdir(book + "/roles") touch(book + "/" + play) touch(book + "/" + "README.md")
def test__run__reboot_failed(self): """ run: reboot failed """ fname = "%s/../reboot-failed" % dirpath sh.touch(fname) self.bundle.run() self.assertEqual(False, os.path.isfile(fname))
def create_role(roles): for role in roles: mkdir("-p", "roles/" + role) mkdir("-p", "roles/" + role + "/files") mkdir("-p", "roles/" + role + "/defaults") mkdir("-p", "roles/" + role + "/templates") mkdir("-p", "roles/" + role + "/tasks") touch("roles/" + role + "/tasks/main.yml") mkdir("-p", "roles/" + role + "/handlers")
def test_get_key(): sh.touch(dotenv_path) success, key_to_set, value_to_set = dotenv.set_key(dotenv_path, 'HELLO', 'WORLD') stored_value = dotenv.get_key(dotenv_path, 'HELLO') assert stored_value == 'WORLD' sh.rm(dotenv_path) assert dotenv.get_key(dotenv_path, 'HELLO') is None success, key_to_set, value_to_set = dotenv.set_key(dotenv_path, 'HELLO', 'WORLD') assert success is None
def init_repo(self, dir): if os.access(dir + ".git", os.F_OK): return else: sh.git("init") self.__ignores() sh.touch("SampleNote.md") sh.git("add", "*.md") sh.git("commit", "-a", m="Initial create.")
def _create_simple_commit(self, message, out=None, ok_code=None, env=None): """ Creates a simple commit with an empty test file. :param message: Commit message for the commit. """ test_filename = "test-file-" + str(uuid4()) touch(test_filename, _cwd=self.tmp_git_repo) git("add", test_filename, _cwd=self.tmp_git_repo) # https://amoffat.github.io/sh/#interactive-callbacks git("commit", "-m", message, _cwd=self.tmp_git_repo, _tty_in=True, _out=out, _ok_code=ok_code, _env=env) return test_filename
def test_run_with_other_env(cli): DOTENV_FILE = 'dotenv' with cli.isolated_filesystem(): sh.cd(here) sh.touch(DOTENV_FILE) sh.dotenv('--file', DOTENV_FILE, 'set', 'FOO', 'BAR') result = sh.dotenv('--file', DOTENV_FILE, 'run', 'printenv', 'FOO').strip() assert result == 'BAR'
def test_empty_file(): filename = 'empty' loop = BooleanLoop() launcher.on_transfer_ended( loop.stop, d_from='rep1', d_to='rep2', filename=filename ) sh.touch(os.path.join(rep1, filename)) loop.run(timeout=5) assert os.path.getsize(os.path.join(rep2, filename)) == 0
def _create_simple_commit(self, message, out=None): """ Creates a simple commit with an empty test file. :param message: Commit message for the commit. """ test_filename = "test-file-" + str(uuid4()) touch(test_filename, _cwd=self.tmp_git_repo) git("add", test_filename, _cwd=self.tmp_git_repo) # https://amoffat.github.io/sh/#interactive-callbacks git("commit", "-m", message, _cwd=self.tmp_git_repo, _tty_in=True, _out=out) return test_filename
def test_load_dotenv(tmp_path): os.chdir(str(tmp_path)) dotenv_path = '.test_load_dotenv' sh.touch(dotenv_path) set_key(dotenv_path, 'DOTENV', 'WORKS') assert 'DOTENV' not in os.environ success = load_dotenv(dotenv_path) assert success assert 'DOTENV' in os.environ assert os.environ['DOTENV'] == 'WORKS'
def test_should_rebase_by_default(self): from sh import touch touch('foo') git('add', 'foo') fix('HEAD') actual = git_show('HEAD')['subject'] expected = 'commit1' self.assertEqual(expected, actual)
def test_doc(): with cd('../../doc'): sys.path.insert(0, os.getcwd()) project.build() touch('_build/.nojekyll') d = git('--no-pager', 'diff', '-w', '--', '_build') if d: print(d) raise ValueError('Diffs in website') _python = None
def setUp(self): self.dir = tempfile.mkdtemp() sh.cd(self.dir) sh.git.init() sh.git('config', 'user.name', '"Daniël"') sh.git('config', 'user.email', '"*****@*****.**"') sh.touch('README') sh.git.add('README') sh.git.commit('-m', 'first commit') sh.git('remote', 'add', 'origin', 'https://github.com/username/Hello-World.git')
def test_subdir_is_made_when_subdir_doesnt_exist(self, tmpdir): # - make a tempdir # - add a file # - make a link from a location in a non-existing subdir to the file with sh.pushd(tmpdir): sh.touch("a.txt") link = "subdir/b.txt" add_relative_symlink("a.txt", link) assert os.path.islink(link) assert os.readlink(link) == "../a.txt"
def cloneWiki(name, cloneUrl): sh.cd("../") sh.mkdir(name + ".wiki") sh.cd(name + ".wiki") git.init() try: git.remote.add(name, cloneUrl[:-4]+ ".wiki.git") sh.touch("home.md") pushAll("Initial Wiki commit") except: print("Wiki remote already exists") sh.cd("../" + name)
def create_plots(dname): """mocking some outputs to move later...""" try: sh.mkdir('-p', dname) for fname in [ dname + DemoPipe.Conf.FILE_A, dname + DemoPipe.Conf.FILE_B, dname + DemoPipe.Conf.FILE_C ]: sh.touch(fname) except: raise
def test_error_if_linkloc_exists_but_is_not_a_link(self, tmpdir): # - make a tempdir # - add two files # - make a link from one file to the other # - making the link should fail since a (non-link) file exists at the # link location with sh.pushd(tmpdir): sh.touch("a.txt") sh.touch("b.txt") with pytest.raises(FileExistsError): add_relative_symlink("a.txt", "b.txt")
def test_load_dotenv(cli): dotenv_path = '.test_load_dotenv' with cli.isolated_filesystem(): sh.touch(dotenv_path) set_key(dotenv_path, 'DOTENV', 'WORKS') assert 'DOTENV' not in os.environ success = load_dotenv(dotenv_path) assert success assert 'DOTENV' in os.environ assert os.environ['DOTENV'] == 'WORKS' sh.rm(dotenv_path)
def test_load_dotenv_override(tmp_path): os.chdir(str(tmp_path)) dotenv_path = '.test_load_dotenv_override' key_name = "DOTENV_OVER" sh.touch(dotenv_path) os.environ[key_name] = "OVERRIDE" set_key(dotenv_path, key_name, 'WORKS') success = load_dotenv(dotenv_path, override=True) assert success assert key_name in os.environ assert os.environ[key_name] == 'WORKS'
def test_should_create_fixup_commit(self): from sh import touch git_commit_empty('foo') touch('foo') git('add', 'foo') fix('-n', 'HEAD') actual = git_show('HEAD')['subject'] expected = 'fixup! foo' self.assertEqual(expected, actual)
def touch(*apps): for appname in apps: vassal = appname + '.json' path = os.path.join(VASSALS, vassal) if not os.path.exists(path): print(colored('App %s not added, ignored.' % appname, 'yellow', attrs=['bold']), file=sys.stderr) else: print('Touching %s:' % appname, end=' ') sh.touch(path) print(colored('ok', 'green', attrs=['bold']) + '.') time.sleep(1)
def setup(self, git_repo_dir): print "SEtting up webconsole in %s" % self.btd sh.mkdir("-p", join(self.btd, "var/log", self.pkg_name)) sh.mkdir("-p", join(self.btd, "var/lib", self.pkg_name)) lib_base = join(self.btd, "usr/lib/appliance-webconsole") sh.mkdir("-p", lib_base) sh.mkdir(join(lib_base, "models")) sh.touch(join(lib_base, "models", "__init__.py")) for subdir, target in [("appliance/web_console/*", ""), ("models/appliancedb.py", "models"), ("models/nodedb.py", "models")]: os.system("cp -r %s %s" %(join(git_repo_dir, "src", subdir), join(lib_base, target)))
def setup(self, git_repo_dir): sh.mkdir("-p", join(self.btd, "var/log", self.pkg_name)) sh.mkdir("-p", join(self.btd, "var/lib", self.pkg_name)) lib_base = join(self.btd, "usr/lib/appliance-discovery") sh.mkdir("-p", lib_base) sh.mkdir(join(lib_base, "models")) sh.touch(join(lib_base, "models", "__init__.py")) for subdir, target in [("appliance/discovery/*", ""), ("models/appliancedb.py", "models"), ("models/nodedb.py", "models")]: os.system("cp -r %s %s" %(join(git_repo_dir, "src", subdir), join(lib_base, target)))
def reboot(self): # TODO: double check web notification with Zack*2 self.publish.event.put( "/system/reboot", data={"code": "REBOOTING", "type": "event"}) sh.touch("%s/rebooting" % self.path_root) sh.sync() # Waiting for web to log out # time.sleep(5) _logger.debug("Turn off the ready led.") subprocess.call(self.set_to_not_ready, shell=True) _logger.info("Rebooting...") returncode = subprocess.call(self.call_reboot, shell=True) if returncode != 0: _logger.info("Reboot failed!") sh.touch("%s/reboot-failed" % self.path_root) sh.sync()
def setUp(self): super(ArchiveRepositoryAuthenticatedMethodsTest, self).setUp() # Clone test repository localy. repo_origin = '[email protected]:%s/%s.git' % (self.bb.username, self.bb.repo_slug) # TODO : Put the temp folder on the right place for windows. repo_folder = os.path.join( '/tmp', 'bitbucket-' + ''.join(random.choice(string.digits + string.letters) for x in range(10))) sh.mkdir(repo_folder) sh.cd(repo_folder) self.pwd = sh.pwd().strip() sh.git.init() sh.git.remote('add', 'origin', repo_origin) # Add commit with empty file. sh.touch('file') sh.git.add('.') sh.git.commit('-m', '"Add empty file."') sh.git.push('origin', 'master')
def test_console_script(cli): with cli.isolated_filesystem(): sh.touch(dotenv_path) sh.dotenv('-f', dotenv_path, 'set', 'HELLO', 'WORLD') output = sh.dotenv('-f', dotenv_path, 'get', 'HELLO', ) assert output == 'HELLO="WORLD"\n' sh.rm(dotenv_path) # should fail for not existing file result = cli.invoke(dotenv.set, ['my_key', 'my_value']) assert result.exit_code != 0 # should fail for not existing file result = cli.invoke(dotenv.get, ['my_key']) assert result.exit_code != 0 # should fail for not existing file result = cli.invoke(dotenv.list, []) assert result.exit_code != 0
def _create_simple_commit(self, message, out=None, ok_code=None, env=None): """ Creates a simple commit with an empty test file. :param message: Commit message for the commit. """ # Let's make sure that we copy the environment in which this python code was executed as environment # variables can influence how git runs. # This was needed to fix https://github.com/jorisroovers/gitlint/issues/15 as we need to make sure to use # the PATH variable that contains the virtualenv's python binary. environment = os.environ if env: environment.update(env) test_filename = "test-file-" + str(uuid4()) touch(test_filename, _cwd=self.tmp_git_repo) git("add", test_filename, _cwd=self.tmp_git_repo) # https://amoffat.github.io/sh/#interactive-callbacks if not ok_code: ok_code = [0] git("commit", "-m", message, _cwd=self.tmp_git_repo, _tty_in=True, _out=out, _ok_code=ok_code, _env=environment) return test_filename