~jcsackett/charmworld/bac-tag-constraints

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
# Copyright 2012, 2013 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Helpers to generate test data for use."""
from bzrlib.bzrdir import BzrDir
from contextlib import contextmanager
from calendar import timegm
from datetime import (
    date,
    datetime,
)
import hashlib
import os
from os.path import basename
from random import randint
import random
import stat
import string
from time import time

from charmworld.charmstore import (
    get_address,
    make_store_url,
)
from charmworld.jobs.ingest import (
    JENKINS_PROVIDERS,
    process_charm,
)
from charmworld.models import (
    Bundle,
    CharmFileSet,
    getfs,
    options_to_storage,
)
from charmworld.utils import write_locked


def random_string(length=None):
    """Generates a random string from urandom.

    :param length: Specify the number of chars in the generated string.
    """
    chars = string.ascii_uppercase + string.digits
    str_length = length if length is not None else randint(0, 100)
    return u''.join(random.choice(chars) for x in range(str_length))


def random_url():
    """Generate a random url that is totally bogus."""
    url = "http://{0}.com".format(random_string())
    return url


def makeUser(name=None, email=None):
    """Generate a user string for changes.

    :param name: The user name; a random string if not provided.
    :param email: The user email; `name`@example.com if not provided.
    """
    if not name:
        name = random_string(10)
    if not email:
        email = '%s@example.com' % name
    return "%s <%s>" % (name, email)


def random_date(start=None, end=None):
    """Generates a random date within the range provided.

    :param start: The start of the range. The first day of the current year if
        not provided.
    :param end: The end of the range. Today if not provided.
    """
    if not start:
        start = date(date.today().year, 1, 1)
    if not end:
        end = date.today()
    increment = ((end.toordinal() - start.toordinal()) * random.random())
    return date.fromordinal(start.toordinal() + int(increment))


def random_datetime():
    return datetime.utcfromtimestamp(random.random() * 1300000000)


def makeChange(committer=None, created=None, message=None, revno=None,
               authors=None):
    """Generate a charm change.

    :param committer: The committer; a random user from `makeUser` if not
        provided.
    :param created: The date the change was committed; a random date if not
        provided.
    :param message: The change's commit message; a random string if not
        provided.
    :param revno: The change's commit's revision number; a random number if
        not provided.
    :param authors: The change's authors, as a list of name/address strings
        as returned by bzrlib.revision.Revision.get_apparent_authors()
    """
    if not committer:
        committer = makeUser()
    if not created:
        created = random_date()
    if not message:
        message = random_string(50)
    if not revno:
        revno = randint(0, 100)
    if authors is None:
        authors = ['Foo <foo@example.com>', 'Bar <bar@example.com>']

    if isinstance(created, date):
        # Change `created` dates should be timestamps.
        created = timegm(created.timetuple())
    return {
        'authors': authors,
        'committer': committer,
        'created': created,
        'message': message,
        'revno': revno}


def get_payload_json(name=None, bname=None, series=None, owner=None,
                     promulgated=False, branch_deleted=False):
    if not series:
        series = 'precise'
    if not bname:
        bname = 'trunk'
    if not owner:
        owner = "charmers"
    if not name:
        name = random_string(55)
    return {
        "bname": bname,
        "branch_deleted": branch_deleted,
        "branch_spec": "~%s/charms/%s/%s/trunk" % (owner, series, name),
        "name": name,
        "owner": owner,
        "promulgated": promulgated,
        "series": series,
    }

def get_charm_json(name=None, changes=None, subordinate=False, tests=None,
                   with_real_files=False, branch_root=None, bname=None,
                   series=None, owner=None, description=None, revision=1,
                   summary=None, revno=12, maintainer=None,
                   commit_message='maintainer', provides=None, requires=None,
                   options=None, files=None, charm_error=False,
                   date_created=None, downloads=0,
                   downloads_in_past_30_days=0, is_featured=False,
                   promulgated=False, categories=None, branch_deleted=False,
                   proof=None, payload=None):
    """Return the json of a charm."""
    if not description:
        description = """byobu-class provides remote terminal access through
            a web browser,\nusing https, ajaxterm, and byobu.  It operates in a
            one-writer,\nmany reader mode.\n"""
    if not changes:
        changes = [makeChange()]
    if date_created is None:
        date_created = random_datetime()
    if branch_root is None:
        if with_real_files:
            branch_root = os.path.split(__file__)[0]
            branch_root = os.path.join(branch_root, 'data')
            name = 'sample_charm'
        else:
            branch_root = '/var/charms/precise/charmers'
    if maintainer is None:
        maintainer = "John Doe <jdoe@example.com>"
    if summary is None:
        summary = "Remote terminal classroom over https"
    if provides is None:
        provides = {
            "website": {
                "interface": "https",
            }
        }
    if requires is None:
        requires = {
            'database': {
                'interface': 'mongodb',
            }
        }
    if options is None:
        options = {
            'script-interval': {
                'type': 'int',
                'default': 5,
                'description': 'The interval between script runs.'
            },
        }
    options = options_to_storage(options)
    if files is None:
        files = {
            'readme': {
                'filename': 'README',
                'subdir': '',
            },
            'install': {
                'filename': 'install',
                'subdir': 'hooks',
            },
        }
    if proof is None:
        proof = {
            "w": [
                " no README file",
                " (install:31) - hook accesses EC2 metadata service directly"
            ]
        }

    if categories is None:
        categories = []
    elif not isinstance(categories, list):
        raise ValueError('Categories must be a list.')
    if payload is None:
        charm = get_payload_json(name, bname, series, owner, promulgated,
                                 branch_deleted)
    else:
        for key in ['name', 'bname', 'series', 'owner']:
            if locals()[key] is not None:
                raise ValueError(
                    'Cannot specify "%s" if payload supplied.' % key)
        charm = dict(payload)
    charm.update({
        '_id': charm['branch_spec'],
        "branch_dir": os.path.join(branch_root, charm['name']),
        "categories": categories,
        "changes": changes,
        "commit": "jdoe@example.com-20120723223205-n5zke9r9ahb40sue",
        "description": description,
        "ensemble": "formula",
        "first_change": {
            "committer": "Jane Doe <jane@sample.com>",
            "created": 1308093138.892,
            "message": "initial checkin\n",
            "revno": 1
        },
        "hooks": [
            "install",
            "start",
            "stop",
            "website-relation-joined"
        ],
        'is_featured': is_featured,
        "last_change": {
            "committer": "John Doe <jdoe@example.com>",
            "created": 1343082725.06,
            "message": commit_message,
            "revno": revno,
        },
        "maintainer": maintainer,
        "proof": proof,
        "provides": provides,
        'requires': requires,
        "revision": revision,
        "summary": summary,
        'config': {
            'options': options,
        },
        'files': files,
        'downloads': downloads,
        'downloads_in_past_30_days': downloads_in_past_30_days,
        'date_created': date_created.isoformat(),
    })
    address = get_address(charm, promulgated)
    charm["store_url"] = make_store_url(1, address)
    store_data = {
        "address": address,
        "store_checked": "Thu Dec  6 17:42:05 2012"
    }
    if charm_error:
        store_data["errors"] = ['entry not found']
    else:
        store_data.update({
            "digest": "jdoe@example.com-20120723223205-n5zke9r9ahb40su",
            "revision": 1,
            "sha256": "107c230dc3481f8b8de9decfff6439a23caae2ea670bddb413ddf1",
        })
    charm['store_data'] = store_data
    if tests is not None:
        charm['tests'] = tests
        charm['test_results'] = {}
        for provider, status in tests.items():
            charm['test_results'][provider] = makeTestResult(
                charm['name'], charm['branch_spec'], status, provider)
    if subordinate:
        charm['subordinate'] = True
    return process_charm(charm)


def makeCharm(db, *args, **kwargs):
    charm = get_charm_json(*args, **kwargs)
    record_id = db.charms.insert(charm)
    return record_id, charm


def get_bundle_data(name='hoopy', owner='', basket=None, inherits=None,
                    series='precise', title='', description='',
                    services=dict(), relations=dict(), promulgated=False,
                    branch_deleted=False):
    return dict(**vars())


def makeBundle(db, *args, **kwargs):
    bundle_data = get_bundle_data(*args, **kwargs)
    bundle = Bundle(bundle_data)
    bundle_data['_id'] = bundle.id
    db.bundles.insert(bundle_data)
    return bundle


def make_charm_file(db, charm, path, content=None):
    fs = getfs(db)
    charm_file = CharmFileSet.make_charm_file(fs, charm, path, basename(path))
    if content is None:
        content = random_string(10).encode('utf-8')
    charm_file.save(content)
    return charm_file


def makeTestResult(db, name, branch_spec, status, provider,
                   branch_revision=None, timestamp=None, test_number=None):
    """Create a test record.
    """
    if provider is None:
        provider = random.choice(JENKINS_PROVIDERS)
    if test_number is None:
        test_number = randint(20, 40)
    if timestamp is None:
        timestamp = int(time()) * 1000
    test_id = '%s::%s-%s' % (branch_spec, provider, test_number)
    test_result = {
        '_id': test_id,
        'branch_spec': branch_spec,
        'number': test_number,
        'provider': provider,
        'result': status,
        'timestamp': timestamp,
        'url': 'https://jenkins.qa.ubuntu.com/job/precise-%s-charm-%s/%s' % (
            provider, name, test_number)
    }
    if branch_revision is None:
        branch_revision = randint(20, 40)
        test_result['revno'] = branch_revision
    db.jenkins.insert(test_result)
    return test_id


def makeSSOResponse(sso_type='login', groups=None):
    if sso_type == 'login':
        payload = {
            u'openid.op_endpoint': u'https://login.ubuntu.com/+openid',
            u'openid.sig': u'o3OoughdEky+xKkryB2USvcwU2w=',
            u'openid.sreg.language': u'en',
            u'openid.return_to': u'http://127.0.0.1/auth_callback?janrain_no',
            u'openid.ns': u'http://specs.openid.net/auth/2.0',
            u'janrain_nonce': u'2012-12-17T18:35:57ZDvqeup',
            u'openid.response_nonce': u'2012-12-17T18:36:42Z3Xqn5M',
            u'openid.ns.sreg': u'http://openid.net/extensions/sreg/1.1',
            u'openid.sreg.fullname': u'Sample Person',
            u'openid.claimed_id': u'https://login.ubuntu.com/+id/MSbTCYM',
            u'openid.mode': u'id_res',
            u'openid.sreg.nickname': u'sample',
            u'openid.sreg.timezone': u'America/Detroit',
            u'openid.signed': u"""assoc_handle,
               claimed_id,
               identity,
               mode,
               ns,
               ns.sreg,
               op_endpoint,
               response_nonce,
               return_to,
               signed,
               sreg.email,
               sreg.fullname,
               sreg.language,
               sreg.nickname,
               sreg.timezone""",
            u'openid.assoc_handle': u'{HMAC-SHA1}{50cf6493}{6QI6Fw==}',
            u'openid.identity': u'https://login.ubuntu.com/+id/MSbTCYM',
            u'openid.sreg.email': u'sample@example.com',
            u'openid.lp.is_member': '',
        }

        if groups:
            payload['openid.lp.is_member'] = ','.join(groups)
        return payload

    elif sso_type == 'cancel':
        return {
            u'janrain_nonce': u'2012-12-17T18:29:39ZKnPAWl',
            u'openid.mode': u'cancel',
            u'openid.ns': u'http://specs.openid.net/auth/2.0',
        }


def makeQAAssessment():
    """Data stored in the qa of a Charm based off the forms/qa_assessment"""
    from collections import OrderedDict
    return OrderedDict([
        ('work_needed_link', u'http://google.com'),
        ('notes', u'lots of notes'),
        ('__start__', u'Reliable:mapping'),
        ('__start__', u'4847e034bb0a55fcbc8a3380d6a3ab80:rename'),
        ('deformField15', u'1'),
        ('__end__', u''),
        ('__start__', u'a60b40bce263c47559d32435a89756fe:rename'),
        ('deformField16', u'1'),
        ('__end__', u''),
        ('__start__', u'7ff6cc997b141399bcdf2c338fb4031c:rename'),
        ('deformField17', u'1'),
        ('__end__', u''),
        ('__start__', u'9a736d9ab5ca1e16ddd942e7a62b8820:rename'),
        ('deformField18', u'0'),
        ('__end__', u''),
        ('__start__', u'51dceb2909262166283ac17f2af504e7:rename'),
        ('deformField19', u'0'),
        ('__end__', u''),
        ('__start__', u'9659919b570c6ef9a25df7261375e6bb:rename'),
        ('deformField20', u'0'),
        ('__end__', u''),
        ('__start__', u'e3c804e2ac59ebb8cb73ac26d43cb260:rename'),
        ('deformField21', u'0'),
        ('__end__', u''),
        ('__start__', u'4cb0090569d839a2bffee6fba06ac2eb:rename'),
        ('__end__', u''),
        ('__start__', u'34e37456112085f7b71b6347f8184a73:rename'),
        ('__end__', u''),
        ('__end__', u'Reliable:mapping'),
        ('__start__', u'Secure:mapping'),
        ('__start__', u'ffa1dc58332d3a0e7a957e2a55007b4f:rename'),
        ('deformField25', u'0'),
        ('__end__', u''),
        ('__start__', u'190e6b028777b602190de1763660ae6f:rename'),
        ('deformField26', u'1'),
        ('__end__', u''),
        ('__start__', u'83d5c7dc3ca94ff5a966099c7c722e08:rename'),
        ('deformField27', u'1'),
        ('__end__', u''),
        ('__start__', u'39e130a521cf04bd9af11138dcdf8d8c:rename'),
        ('deformField28', u'0'),
        ('__end__', u''),
        ('__end__', u'Secure:mapping'),
        ('__start__', u'Flexible:mapping'),
        ('__start__', u'3a57079d741ccc48e22db219b0aec68e:rename'),
        ('deformField30', u'0'),
        ('__end__', u''),
        ('__start__', u'c913e2509bcbf1cc027e95dae1f8ba7f:rename'),
        ('deformField31', u'1'),
        ('__end__', u''),
        ('__end__', u'Flexible:mapping'),
        ('__start__', u'Handle Data:mapping'),
        ('__start__', u'69be998e2aee961adfb12773c6796957:rename'),
        ('deformField33', u'0'),
        ('__end__', u''),
        ('__start__', u'afc32c74c6b99b784b41af5e44d43981:rename'),
        ('deformField34', u''),
        ('__end__', u''),
        ('__start__', u'afc32c74c6b99b784b41af5e44d43981:rename'),
        ('deformField35', u''),
        ('__end__', u''),
        ('__end__', u'Handle Data:mapping'),
        ('__start__', u'Scaleable:mapping'),
        ('__start__', u'49eb3b0d38a795f41c59d62d46a3b887:rename'),
        ('deformField37', u'0'),
        ('__end__', u''),
        ('__start__', u'd048cfb0691d53c6eb3aced71340cab3:rename'),
        ('deformField38', u'1'),
        ('__end__', u''),
        ('__start__', u'f081c5d19281a7decfd06bf754c82386:rename'),
        ('deformField39', u'1'),
        ('__end__', u''),
        ('__start__', u'191a39c1b62a1ab8c97af36eb23e4af8:rename'),
        ('deformField40', u'1'),
        ('__end__', u''),
        ('__start__', u'b10cadcaecfc455cf5b5c4521edb7d86:rename'),
        ('deformField41', u'0'),
        ('__end__', u''),
        ('__end__', u'Scaleable:mapping'),
        ('__start__', u'Easy to Deploy:mapping'),
        ('__start__', u'899c01c6ba4103348c71b968d9c6b91b:rename'),
        ('deformField43', u'0'),
        ('__end__', u''),
        ('__start__', u'98331d802f2ff09e4dd256d0b488c20d:rename'),
        ('deformField44', u'0'),
        ('__end__', u''),
        ('__start__', u'419f40223e91f5bb1f6a734b800d2f1d:rename'),
        ('deformField45', u'0'),
        ('__end__', u''),
        ('__start__', u'9bcf839c9b5b85974b5a68b65e1784d3:rename'),
        ('deformField46', u'1'),
        ('__end__', u''),
        ('__start__', u'1fc3e7fe66dca5d512a54ee178336f78:rename'),
        ('deformField47', u'1'),
        ('__end__', u''),
        ('__start__', u'77dd5c463f0c4bc455633307c87957cf:rename'),
        ('deformField48', u'1'),
        ('__end__', u''),
        ('__start__', u'64a0c6cd62f82f9a2a2f2971e8732f39:rename'),
        ('deformField49', u'0'),
        ('__end__', u''),
        ('__end__', u'Easy to Deploy:mapping'),
        ('__start__', u'Responsive to DevOps Needs:mapping'),
        ('__start__', u'6d74092ba1b34019f7276141820fcac6:rename'),
        ('deformField51', u'1'),
        ('__end__', u''),
        ('__start__', u'348be0e0e93d66d3084c8709dffc260e:rename'),
        ('deformField52', u'0'),
        ('__end__', u''),
        ('__start__', u'76634ca7dc25e6071ff327b78209fbc2:rename'),
        ('deformField53', u'0'),
        ('__end__', u''),
        ('__start__', u'9293ffcaf0cca86c793bd92faf21360f:rename'),
        ('deformField54', u'1'),
        ('__end__', u''),
        ('__end__', u'Responsive to DevOps Needs:mapping'),
        ('__start__', u'Upstream Friendly:mapping'),
        ('__start__', u'c0b6382136022a6fdc6c20a6ffc45ca5:rename'),
        ('deformField56', u'0'),
        ('__end__', u''),
        ('__start__', u'5fd70986787dae347b7a579f8c25fcd2:rename'),
        ('deformField57', u'1'),
        ('__end__', u''),
        ('__start__', u'375baad73faa59703208fccd84640ded:rename'),
        ('deformField58', u'1'),
        ('__end__', u''),
        ('__start__', u'ca575c0c1fce388c18b1cd13fa14e293:rename'),
        ('deformField59', u'1'),
        ('__end__', u''),
        ('__end__', u'Upstream Friendly:mapping'),
    ])


def makeQAScore():
    return {
        "data_handling": {
            "data_handling_2": "1",
            "data_handling_0": "1",
            "data_handling_1": "0"
        },
        "secure": {
            "secure_2": "0",
            "secure_3": "0",
            "secure_0": "1",
            "secure_1": "1"
        },
        "notes": "More work",
        "scalable": {
            "scalable_2": "0",
            "scalable_3": "0",
            "scalable_0": "1",
            "scalable_1": "1",
            "scalable_4": "0"
        },
        "responsive": {
            "responsive_3": "0",
            "responsive_2": "1",
            "responsive_1": "1",
            "responsive_0": "1"
        },
        "upstream": {
            "upstream_0": "1",
            "upstream_1": "1",
            "upstream_2": "0",
            "upstream_3": "0"
        },
        "work_needed_link": "Need work",
        "flexible": {
            "flexible_0": "1",
            "flexible_1": "0"
        },
        "easy_deploy": {
            "easy_deploy_0": "1",
            "easy_deploy_1": "1",
            "easy_deploy_2": "1",
            "easy_deploy_3": "0",
            "easy_deploy_4": "0",
            "easy_deploy_5": "0",
            "easy_deploy_6": "0"
        },
        "reliable": {
            "reliable_1": "1",
            "reliable_0": "1",
            "reliable_3": "0",
            "reliable_2": "0",
            "reliable_5": "1",
            "reliable_4": "",
            "reliable_7": "0",
            "reliable_6": "1",
            "reliable_8": "0"
        }
    }


def makeQAQuestionGroup():
    """Generate the dict needed for the qa collection."""

    category = (
        u'reliable',
        u'Reliable',
        [
            (u'AWS', 1, u''),
            (u'HP Cloud', 1, u''),
            (u'OpenStack', 1, u''),
            (u'LXC', 1, u''),
            (u'MAAS', 1, u''),
            (u'Check for integrity from upstream source', 1, u''),
            (u'Fail gracefully if upstream source goes missing', 1, u''),
            (u'Contain a suite of tests with the charm that pass', 1, u''),
            (u'Passes tests from Jenkins on jujucharms.com', 1, u''),
        ]
    )

    category_dict = {
        'name': category[0],
        'description': category[1],
        'questions': [{
            'id': hashlib.md5(q[0]).hexdigest(),
            'description': q[0],
            'points': q[1],
            'extended_description': q[2]
        } for q in category[2]]
    }

    return category_dict


def make_review_entry(**kwargs):
    """Create an entry for the review queue.

    keyword arguments override the defaults.
    """
    entry = {
        'date_modified': random_datetime(),
        'date_created': random_datetime(),
        'summary': random_string(10),
        'item': random_string(10),
        'status': 'Needs review',
    }
    entry.update(kwargs)
    return entry


@contextmanager
def charm_branch(charm_path=None, extra=[]):
    """Create a Bazaar branch from the files in charm_path."""
    # Avoid circular imports.
    from charmworld.jobs.tests.test_bzr import bzr_isolation
    if charm_path is None:
        charm_path = os.path.split(__file__)[0]
        charm_path = os.path.join(charm_path, 'data/sample_charm')
    with bzr_isolation() as cwd:
        branch_dir = os.path.join(cwd, 'sample_charm/trunk')
        os.makedirs(branch_dir)
        tree = BzrDir.create_standalone_workingtree(branch_dir)
        with write_locked(tree):
            for dirpath, dirnames, filenames in os.walk(charm_path):
                src_paths = [os.path.join(dirpath, name) for name in dirnames]
                rel_paths = [
                    os.path.relpath(path, charm_path) for path in src_paths]
                dst_paths = [
                    os.path.join(branch_dir, path) for path in rel_paths]
                for src, dst in zip(src_paths, dst_paths):
                    if stat.S_ISLNK(os.lstat(src).st_mode):
                        os.symlink(os.readlink(src), dst)
                    else:
                        os.mkdir(dst)
                tree.add(rel_paths)
                src_paths = [os.path.join(dirpath, name) for name in filenames]
                rel_paths = [
                    os.path.relpath(path, charm_path) for path in src_paths]
                dst_paths = [
                    os.path.join(branch_dir, path) for path in rel_paths]
                for src, dst in zip(src_paths, dst_paths):
                    if stat.S_ISLNK(os.lstat(src).st_mode):
                        os.symlink(os.readlink(src), dst)
                    else:
                        with open(dst, 'w') as out:
                            out.write(open(src).read())
                tree.add(rel_paths)

            revision = tree.commit('commit 1')
            for func in extra:
                result = func(cwd, tree)
                if result is not None:
                    revision = result
        yield branch_dir, revision