Best Python code snippet using autotest_python
deploy.py
Source:deploy.py
...50def get_repo_dir(config: str) -> str:51 return f'{PYTHON_DIR}/newdjangosite-{config}'525354def get_backend_dir(repo_dir: str) -> str:55 return f'{repo_dir}/back'565758def get_frontend_dir(repo_dir: str) -> str:59 return f'{repo_dir}/front'606162def get_virtualenv_python_bin(repo_dir: str) -> str:63 return f'{repo_dir}/venv/bin/python'646566def get_secret_repo_dir(config: str) -> str:67 return f'{PYTHON_DIR}/newdjangosite-{config}-secrets'686970def get_secret_repo_name(config: str) -> str:71 configuration = CONFIGURATIONS[config]72 return configuration['secret_repo_name']737475def get_secret_repo_branch(config: str, branch_override: Optional[str] = None) -> str:76 if branch_override:77 return branch_override7879 configuration = CONFIGURATIONS[config]80 return configuration['secret_repo_branch']818283@Task84def deploy(conn, config, branch=None, secret_branch=None):85 configuration = CONFIGURATIONS[config]86 if not branch:87 branch = configuration['branch']88 secret_branch = get_secret_repo_branch(config, secret_branch)89 use_ssl = configuration['ssl']9091 print(Fore.GREEN + f'Deploying {config} from branch {branch} with ' +92 f'secret repo from branch {secret_branch}')9394 repo_dir = get_repo_dir(config)95 config_dir = f'{repo_dir}/config/ubuntu-22.04'96 daily_scripts_dir = f'{config_dir}/cron.daily'97 uwsgi_dir = f'{config_dir}/uwsgi'98 nginx_dir = f'{config_dir}/nginx'99 secret_repo_dir = get_secret_repo_dir(config)100 ssl_dir = f'{secret_repo_dir}/{config}/ssl'101102 _update_source(conn, repo_dir, branch)103 _update_source(conn, secret_repo_dir, secret_branch)104 update_backend_dependencies(conn, repo_dir)105 _compile_source(conn, config, repo_dir)106 _update_scripts(conn, config, daily_scripts_dir)107 _update_database(conn, config, repo_dir)108 _reload_code(conn, config, uwsgi_dir)109 _build_content(conn, repo_dir)110 _reload_web(conn, config, nginx_dir, use_ssl, ssl_dir)111 _run_tests(conn, config, repo_dir)112113 print(Fore.GREEN + f'Deployment to {config} complete')114115116def _update_source(conn: Connection, repo_dir: str, branch: str):117 print(Fore.GREEN + f'update_source for {repo_dir}')118 ensure_directory(conn, repo_dir, owning_group=WEBADMIN_GROUP, mod='ug+w')119 with conn.cd(repo_dir):120 conn.run('sudo git fetch origin')121 conn.run(f'sudo git reset --hard origin/{branch}')122123124def update_backend_dependencies(conn: Connection, repo_dir: str):125 print(Fore.GREEN + 'update_backend_dependencies')126127 venv_dir = f'{repo_dir}/venv'128 if not exists(conn, venv_dir):129 print(Fore.GREEN + 'Creating virtualenv')130 conn.run(f'python3 -m venv --system-site-packages {venv_dir}')131132 with conn.cd(repo_dir):133 print(Fore.GREEN + 'Updating pip')134 conn.run('venv/bin/python -m pip install --upgrade pip')135136 print(Fore.GREEN + 'Updating pip-tools')137 conn.run('venv/bin/python -m pip install --upgrade pip-tools')138139 print(Fore.GREEN + 'Installing dependencies with pip-sync')140 conn.run('venv/bin/pip-sync ubuntu64-py310-requirements.txt')141142143def _compile_source(conn: Connection,144 config: str,145 repo_dir: str):146 print(Fore.GREEN + 'compile_source')147 backend_dir = get_backend_dir(repo_dir)148 python_bin = get_virtualenv_python_bin(repo_dir)149150 with conn.cd(backend_dir):151 conn.run('sudo find . -iname "*.pyc" -delete')152 conn.run(f'sudo {python_bin} -m compileall .')153 conn.run(f'sudo {python_bin} manage_{config}.py collectstatic --noinput')154155156def _update_scripts(conn: Connection, config: str, daily_scripts_dir: str):157 print(Fore.GREEN + 'update_scripts')158 cron_daily_dir = '/etc/cron.daily'159 with conn.cd(daily_scripts_dir):160 conn.run(f'sudo cp newdjangosite-{config}-* {cron_daily_dir}')161162 with conn.cd(cron_daily_dir):163 conn.run(f'sudo chmod 755 newdjangosite-{config}-*')164165166def _update_database(conn: Connection, config: str, repo_dir: str):167 print(Fore.GREEN + 'update_database')168 backend_dir = get_backend_dir(repo_dir)169 python_bin = get_virtualenv_python_bin(repo_dir)170 with conn.cd(backend_dir):171 conn.run(f'sudo {python_bin} manage_{config}.py migrate')172173174def _reload_code(conn: Connection, config: str, uwsgi_dir: str):175 print(Fore.GREEN + 'reload_code')176 with conn.cd(uwsgi_dir):177 conn.run(f'sudo cp newdjangosite-{config}.ini /etc/uwsgi/apps-available')178 conn.run(f'sudo chmod 644 /etc/uwsgi/apps-available/newdjangosite-{config}.ini')179 conn.run(f'sudo systemctl enable uwsgi-app@newdjangosite-{config}.socket')180 conn.run(f'sudo systemctl enable uwsgi-app@newdjangosite-{config}.service')181 conn.run(f'sudo systemctl start uwsgi-app@newdjangosite-{config}.socket')182 conn.run(f'sudo touch /var/uwsgi/newdjangosite-{config}.reload')183184185def _build_content(conn: Connection, repo_dir: str):186 angular_dir = get_frontend_dir(repo_dir)187 print(Fore.GREEN + 'build_content')188 with conn.cd(angular_dir):189 conn.run('sudo npm install --location=global npm@8')190 conn.run('sudo npm install --location=global @angular/cli')191 conn.run('sudo npm ci')192 conn.run('sudo npm run-script build')193 conn.run('sudo rsync -va dist/front built')194195196def _reload_web(conn: Connection, config: str, nginx_dir: str, ssl: bool, ssl_dir: str):197 print(Fore.GREEN + 'reload_web')198 with conn.cd(nginx_dir):199 conn.run(f'sudo cp {config}.yourdomain.tld /etc/nginx/sites-enabled/')200201 if ssl:202 with conn.cd(ssl_dir):203 conn.run(f'sudo cp {config}.yourdomain.tld.* /etc/nginx/ssl')204 conn.run(f'sudo chown root /etc/nginx/ssl/{config}.yourdomain.tld.*')205 conn.run(f'sudo chgrp root /etc/nginx/ssl/{config}.yourdomain.tld.*')206 conn.run(f'sudo chmod 644 /etc/nginx/ssl/{config}.yourdomain.tld.*')207208 conn.sudo('/etc/init.d/nginx reload')209210211def _run_tests(conn: Connection, config: str, repo_dir: str):212 print(Fore.GREEN + 'run_tests')213 backend_dir = get_backend_dir(repo_dir)214 python_bin = get_virtualenv_python_bin(repo_dir)215 with conn.cd(backend_dir):216 conn.run(f'{python_bin} manage_{config}.py test')217218219def checkout_branch(conn: Connection, repo_dir: str, config: str, branch: Optional[str] = None):220 if not branch:221 configuration = CONFIGURATIONS[config]222 branch = configuration['branch']223224 _update_source(conn, repo_dir, branch)225226227@Task
...
backend.py
Source:backend.py
...26 except ValueError:27 return False28def _normalize(string: str) -> str:29 return string.replace('-', '_')30def get_backend_dir(backend: str) -> Path:31 return backends_dir / _normalize(backend)32def parse_backend_string(backend_string: str) -> tuple[Path | None, str]:33 if '/' in backend_string:34 backend, _, package_name = backend_string.partition('/')35 backend_dir = get_backend_dir(backend)36 if not backend_dir.is_dir():37 raise BackendError(38 f'{backend_dir} does not exist or is not a directory')39 else:40 package_name = backend_string41 backend_dir = get_backend_dir(backend_string)42 if not backend_dir.is_dir():43 backend_dir = None44 return backend_dir, _normalize(package_name)45def _init_backend(backend_string: str) -> Path | None:46 backend_dir, package_name = parse_backend_string(backend_string)47 if backend_dir:48 sys.path.insert(0, str(backend_dir))49 ytdl.init(package_name)50 return backend_dir51def _autodetect_backend() -> Path | None:52 for candidate in _AUTODETECT_CANDIDATES:53 try:54 return _init_backend(candidate)55 except DLPlusException:...
install.py
Source:install.py
...20 help='Force installation if the same version is already installed.'21 ),22 )23 def get_output_dir(self, wheel: Wheel) -> Path:24 return backend.get_backend_dir(wheel.name)25 def get_name_version_tuple(self) -> Tuple[str, Optional[str]]:26 return (self.args.name, self.args.version)27 def get_force_flag(self) -> bool:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!