| Conditions | 14 |
| Total Lines | 133 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like generate_files() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # -*- coding: utf-8 -*- |
||
| 247 | def generate_files(repo_dir, context=None, output_dir='.', |
||
| 248 | overwrite_if_exists=False): |
||
| 249 | """Render the templates and saves them to files. |
||
| 250 | |||
| 251 | :param repo_dir: Project template input directory. |
||
| 252 | :param context: Dict for populating the template's variables. |
||
| 253 | :param output_dir: Where to output the generated project dir into. |
||
| 254 | :param overwrite_if_exists: Overwrite the contents of the output directory |
||
| 255 | if it exists. |
||
| 256 | """ |
||
| 257 | template_dir = find_template(repo_dir) |
||
| 258 | logger.debug('Generating project from {}...'.format(template_dir)) |
||
| 259 | context = context or {} |
||
| 260 | |||
| 261 | unrendered_dir = os.path.split(template_dir)[1] |
||
| 262 | ensure_dir_is_templated(unrendered_dir) |
||
| 263 | env = StrictEnvironment( |
||
| 264 | context=context, |
||
| 265 | keep_trailing_newline=True, |
||
| 266 | ) |
||
| 267 | try: |
||
| 268 | project_dir, output_directory_created = render_and_create_dir( |
||
| 269 | unrendered_dir, |
||
| 270 | context, |
||
| 271 | output_dir, |
||
| 272 | env, |
||
| 273 | overwrite_if_exists |
||
| 274 | ) |
||
| 275 | except UndefinedError as err: |
||
| 276 | msg = "Unable to create project directory '{}'".format(unrendered_dir) |
||
| 277 | raise UndefinedVariableInTemplate(msg, err, context) |
||
| 278 | |||
| 279 | # We want the Jinja path and the OS paths to match. Consequently, we'll: |
||
| 280 | # + CD to the template folder |
||
| 281 | # + Set Jinja's path to '.' |
||
| 282 | # |
||
| 283 | # In order to build our files to the correct folder(s), we'll use an |
||
| 284 | # absolute path for the target folder (project_dir) |
||
| 285 | |||
| 286 | project_dir = os.path.abspath(project_dir) |
||
| 287 | logger.debug('Project directory is {}'.format(project_dir)) |
||
| 288 | |||
| 289 | # if we created the output directory, then it's ok to remove it |
||
| 290 | # if rendering fails |
||
| 291 | delete_project_on_failure = output_directory_created |
||
| 292 | |||
| 293 | _run_hook_from_repo_dir( |
||
| 294 | repo_dir, |
||
| 295 | 'pre_gen_project', |
||
| 296 | project_dir, |
||
| 297 | context, |
||
| 298 | delete_project_on_failure |
||
| 299 | ) |
||
| 300 | |||
| 301 | with work_in(template_dir): |
||
| 302 | env.loader = FileSystemLoader('.') |
||
| 303 | |||
| 304 | for root, dirs, files in os.walk('.'): |
||
| 305 | # We must separate the two types of dirs into different lists. |
||
| 306 | # The reason is that we don't want ``os.walk`` to go through the |
||
| 307 | # unrendered directories, since they will just be copied. |
||
| 308 | copy_dirs = [] |
||
| 309 | render_dirs = [] |
||
| 310 | |||
| 311 | for d in dirs: |
||
| 312 | d_ = os.path.normpath(os.path.join(root, d)) |
||
| 313 | # We check the full path, because that's how it can be |
||
| 314 | # specified in the ``_copy_without_render`` setting, but |
||
| 315 | # we store just the dir name |
||
| 316 | if is_copy_only_path(d_, context): |
||
| 317 | copy_dirs.append(d) |
||
| 318 | else: |
||
| 319 | render_dirs.append(d) |
||
| 320 | |||
| 321 | for copy_dir in copy_dirs: |
||
| 322 | indir = os.path.normpath(os.path.join(root, copy_dir)) |
||
| 323 | outdir = os.path.normpath(os.path.join(project_dir, indir)) |
||
| 324 | logger.debug( |
||
| 325 | 'Copying dir {} to {} without rendering' |
||
| 326 | ''.format(indir, outdir) |
||
| 327 | ) |
||
| 328 | shutil.copytree(indir, outdir) |
||
| 329 | |||
| 330 | # We mutate ``dirs``, because we only want to go through these dirs |
||
| 331 | # recursively |
||
| 332 | dirs[:] = render_dirs |
||
| 333 | for d in dirs: |
||
| 334 | unrendered_dir = os.path.join(project_dir, root, d) |
||
| 335 | try: |
||
| 336 | render_and_create_dir( |
||
| 337 | unrendered_dir, |
||
| 338 | context, |
||
| 339 | output_dir, |
||
| 340 | env, |
||
| 341 | overwrite_if_exists |
||
| 342 | ) |
||
| 343 | except UndefinedError as err: |
||
| 344 | if delete_project_on_failure: |
||
| 345 | rmtree(project_dir) |
||
| 346 | _dir = os.path.relpath(unrendered_dir, output_dir) |
||
| 347 | msg = "Unable to create directory '{}'".format(_dir) |
||
| 348 | raise UndefinedVariableInTemplate(msg, err, context) |
||
| 349 | |||
| 350 | for f in files: |
||
| 351 | infile = os.path.normpath(os.path.join(root, f)) |
||
| 352 | if is_copy_only_path(infile, context): |
||
| 353 | outfile_tmpl = env.from_string(infile) |
||
| 354 | outfile_rendered = outfile_tmpl.render(**context) |
||
| 355 | outfile = os.path.join(project_dir, outfile_rendered) |
||
| 356 | logger.debug( |
||
| 357 | 'Copying file {} to {} without rendering' |
||
| 358 | ''.format(infile, outfile) |
||
| 359 | ) |
||
| 360 | shutil.copyfile(infile, outfile) |
||
| 361 | shutil.copymode(infile, outfile) |
||
| 362 | continue |
||
| 363 | try: |
||
| 364 | generate_file(project_dir, infile, context, env) |
||
| 365 | except UndefinedError as err: |
||
| 366 | if delete_project_on_failure: |
||
| 367 | rmtree(project_dir) |
||
| 368 | msg = "Unable to create file '{}'".format(infile) |
||
| 369 | raise UndefinedVariableInTemplate(msg, err, context) |
||
| 370 | |||
| 371 | _run_hook_from_repo_dir( |
||
| 372 | repo_dir, |
||
| 373 | 'post_gen_project', |
||
| 374 | project_dir, |
||
| 375 | context, |
||
| 376 | delete_project_on_failure |
||
| 377 | ) |
||
| 378 | |||
| 379 | return project_dir |
||
| 380 |