Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

#!/usr/bin/python3 

 

""" 

pylucid bootstrap 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

 

A interactive shell for booting the 'pylucid' project. 

 

Note: 

- This file is "self contained". 

- It used **only** stuff from Python lib. 

- So it's "run able" on a bare python 3 installation 

- On debian / ubuntu the 'python3-venv' package is needed! 

 

usage, e.g.: 

 

$ wget https://raw.githubusercontent.com/jedie/PyLucid/master/pylucid/boot_pylucid.py 

$ python3 boot_pylucid.py 

 

pylucid_boot> boot ~/pylucid-env 

 

NOTE: 

* This file is generated via cookiecutter! 

* Don't edit it directly! 

 

* The source file can be found here: 

https://github.com/jedie/bootstrap_env/blob/master/bootstrap_env/boot_source/ 

 

* Create issues about this file here: 

https://github.com/jedie/bootstrap_env/issues 

 

* Pull requests are welcome ;) 

 

:created: 11.03.2018 by Jens Diemer, www.jensdiemer.de 

:copyleft: 2018 by the bootstrap_env team, see AUTHORS for more details. 

:license: GNU General Public License v3 or later (GPLv3+), see LICENSE for more details. 

""" 

 

import cmd 

import logging 

import os 

import pathlib 

import subprocess 

import sys 

import time 

import traceback 

from pathlib import Path 

 

49 ↛ 50line 49 didn't jump to line 50, because the condition on line 49 was never trueif sys.version_info < (3, 5): 

print("\nERROR: Python 3.5 or greater is required!") 

print("(Current Python Verison is %s)\n" % sys.version.split(" ",1)[0]) 

sys.exit(101) 

 

 

try: 

import venv 

except ImportError as err: 

# e.g.: debian / ubuntu doesn't have venv installed, isn't it?!? 

print("\nERROR: 'venv' not available: %s (Maybe 'python3-venv' package not installed?!?)" % err) 

 

 

try: 

import ensurepip 

except ImportError as err: 

# e.g.: debian / ubuntu doesn't have venv installed, isn't it?!? 

print("\nERROR: 'ensurepip' not available: %s (Maybe 'python3-venv' package not installed?!?)" % err) 

 

 

__version__ = "1.0.0rc18" # Version from used 'bootstrap_env' to generate this file. 

 

 

log = logging.getLogger(__name__) 

 

 

PACKAGE_NAME="pylucid" # PyPi package name 

 

# admin shell console script entry point name ('setup.py 

# (used to call 'upgrade_requirements' after virtualenv creation) 

# It's the 'scripts' keyword argument in project 'setup.py' 

# see: 

# https://python-packaging.readthedocs.io/en/latest/command-line-scripts.html#the-scripts-keyword-argument 

# 

ADMIN_FILE_NAME="pylucid_admin.py" # File under .../<project>/foobar_admin.py 

 

# Note: 

# on 'master' branch: '--pre' flag must not be set: So the last release on PyPi will be installed. 

# on 'develop' branch: set the '--pre' flag and publish 'preview' versions on PyPi. 

# 

DEVELOPER_INSTALL=["-e", "git+https://github.com/jedie/PyLucid.git@master#egg=%s" % PACKAGE_NAME] 

NORMAL_INSTALL=[ 

"--pre", # https://pip.pypa.io/en/stable/reference/pip_install/#pre-release-versions 

PACKAGE_NAME 

] 

 

SELF_FILE_PATH=Path(__file__).resolve() # .../src/bootstrap-env/bootstrap_env/boot_bootstrap_env.py 

ROOT_PATH=Path(SELF_FILE_PATH, "..", "..").resolve() # .../src/bootstrap_env/ 

OWN_FILE_NAME=SELF_FILE_PATH.name # boot_bootstrap_env.py 

 

# print("SELF_FILE_PATH: %s" % SELF_FILE_PATH) 

# print("ROOT_PATH: %s" % ROOT_PATH) 

# print("OWN_FILE_NAME: %s" % OWN_FILE_NAME) 

 

 

def in_virtualenv(): 

# Maybe this is not the best way?!? 

return "VIRTUAL_ENV" in os.environ 

 

 

109 ↛ 112line 109 didn't jump to line 112, because the condition on line 109 was never falseif in_virtualenv(): 

print("Activated virtualenv detected: %r (%s)" % (sys.prefix, sys.executable)) 

else: 

print("We are not in a virtualenv, ok.") 

 

 

SUBPROCESS_TIMEOUT=60 # default timeout for subprocess calls 

 

 

 

class Colorizer: 

""" 

Borrowed from Django: 

https://github.com/django/django/blob/master/django/utils/termcolors.py 

 

>>> c = Colorizer() 

>>> c._supports_colors() 

True 

>>> c.color_support = True 

>>> c.colorize('no color') 

'no color' 

>>> c.colorize('bold', opts=("bold",)) 

'\\x1b[1mbold\\x1b[0m' 

>>> c.colorize("colors!", foreground="red", background="blue", opts=("bold", "blink")) 

'\\x1b[31;44;1;5mcolors!\\x1b[0m' 

""" 

def __init__(self, stdout=sys.stdout, stderr=sys.stderr): 

self._stdout = stdout 

self._stderr = stderr 

 

color_names = ('black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white') 

 

self._foreground_colors = dict([(color_names[x], '3%s' % x) for x in range(8)]) 

self._background_colors = dict([(color_names[x], '4%s' % x) for x in range(8)]) 

self._opt_dict = {'bold': '1', 'underscore': '4', 'blink': '5', 'reverse': '7', 'conceal': '8'} 

 

self.color_support = self._supports_colors() 

 

def _supports_colors(self): 

148 ↛ 149line 148 didn't jump to line 149, because the condition on line 148 was never true if sys.platform in ('win32', 'Pocket PC'): 

return False 

 

# isatty is not always implemented! 

152 ↛ 153line 152 didn't jump to line 153, because the condition on line 152 was never true if hasattr(self._stdout, 'isatty') and self._stdout.isatty(): 

return True 

else: 

return False 

 

def colorize(self, text, foreground=None, background=None, opts=()): 

""" 

Returns your text, enclosed in ANSI graphics codes. 

""" 

161 ↛ 164line 161 didn't jump to line 164, because the condition on line 161 was never false if not self.color_support: 

return text 

 

code_list = [] 

 

if foreground: 

code_list.append(self._foreground_colors[foreground]) 

if background: 

code_list.append(self._background_colors[background]) 

 

for option in opts: 

code_list.append(self._opt_dict[option]) 

 

if not code_list: 

return text 

 

return "\x1b[%sm%s\x1b[0m" % (';'.join(code_list), text) 

 

def _out_err(self, func, *args, flush=False, **kwargs): 

text = self.colorize(*args, **kwargs) 

func.write("%s\n" % text) 

if flush: 

func.flush() 

 

def out(self, *args, flush=False, **kwargs): 

""" colorize and print to stdout """ 

self._out_err(self._stdout, *args, flush=flush, **kwargs) 

 

def err(self, *args, flush=False, **kwargs): 

""" colorize and print to stderr """ 

self._out_err(self._stderr, *args, flush=flush, **kwargs) 

 

def demo(self): 

for background_color in sorted(self._background_colors.keys()): 

line = ["%10s:" % background_color] 

for foreground_color in sorted(self._foreground_colors.keys()): 

line.append( 

self.colorize(" %s " % foreground_color, 

foreground=foreground_color, background=background_color 

) 

) 

 

for opt in sorted(self._opt_dict.keys()): 

line.append( 

self.colorize(" %s " % opt, 

background=background_color, opts=(opt,) 

) 

) 

 

self.out("".join(line), background=background_color) 

 

 

colorizer = Colorizer() 

# colorizer.demo() 

 

 

 

class VerboseSubprocess: 

""" 

Verbose Subprocess 

""" 

def __init__(self, *popenargs, env_updates=None, timeout=SUBPROCESS_TIMEOUT, universal_newlines=True, stderr=subprocess.STDOUT, **kwargs): 

""" 

:param popenargs: 'args' for subprocess.Popen() 

:param env_updates: dict to overwrite os.environ. 

:param timeout: pass to subprocess.Popen() 

:param kwargs: pass to subprocess.Popen() 

""" 

 

# subprocess doesn't accept Path() objects 

for arg in popenargs: 

assert not isinstance(arg, pathlib.Path), "Arg %r not accepted!" % arg 

for key, value in kwargs.items(): 

assert not isinstance(value, pathlib.Path), "Keyword argument %r: %r not accepted!" % (key, value) 

 

self.popenargs = popenargs 

self.kwargs = kwargs 

 

self.kwargs["timeout"] = timeout 

self.kwargs["universal_newlines"] = universal_newlines 

self.kwargs["stderr"] = stderr 

self.kwargs["bufsize"] = -1 

 

self.args_str = " ".join([str(x) for x in self.popenargs]) 

 

env = self.kwargs.get("env", os.environ.copy()) 

env["PYTHONUNBUFFERED"]="1" # If a python script called ;) 

 

self.env_updates = env_updates 

250 ↛ 251line 250 didn't jump to line 251, because the condition on line 250 was never true if self.env_updates is not None: 

env.update(env_updates) 

 

self.kwargs["env"] = env 

 

def print_call_info(self): 

print("") 

print("_"*79) 

 

kwargs_txt=[] 

for key, value in self.kwargs.items(): 

if key == "env": 

continue 

key = colorizer.colorize(key, foreground="magenta", opts=("bold",)) 

value = colorizer.colorize(value, foreground="green", opts=("bold",)) 

kwargs_txt.append("%s=%s" % (key, value)) 

 

txt = "Call: '{args}' with: {kwargs}".format( 

args=colorizer.colorize(self.args_str, foreground="cyan", opts=("bold",)), 

kwargs=", ".join(kwargs_txt) 

) 

 

272 ↛ 273line 272 didn't jump to line 273, because the condition on line 272 was never true if self.env_updates is not None: 

txt += colorizer.colorize(" env:", foreground="magenta", opts=("bold",)) 

txt += colorizer.colorize(repr(self.env_updates), opts=("bold",)) 

 

print(txt) 

print("", flush=True) 

 

def print_exit_code(self, exit_code): 

txt = "\nExit code %r from %r\n" % (exit_code, self.args_str) 

281 ↛ 282line 281 didn't jump to line 282, because the condition on line 281 was never true if exit_code: 

colorizer.err(txt, foreground="red", flush=True) 

else: 

colorizer.out(txt, foreground="green", flush=True) 

 

def verbose_call(self, check=True): 

""" 

run subprocess.call() 

 

:param check: if True and subprocess exit_code !=0: sys.exit(exit_code) after run. 

:return: process exit code 

""" 

self.print_call_info() 

 

try: 

exit_code = subprocess.call(self.popenargs, **self.kwargs) 

except KeyboardInterrupt: 

print("\nExit %r\n" % self.args_str, flush=True) 

exit_code=None # good idea?!? 

 

sys.stderr.flush() 

 

self.print_exit_code(exit_code) 

304 ↛ 305line 304 didn't jump to line 305, because the condition on line 304 was never true if check and exit_code: 

sys.exit(exit_code) 

 

return exit_code 

 

def verbose_output(self, check=True): 

""" 

run subprocess.check_output() 

 

:param check: if True and subprocess exit_code !=0: sys.exit(exit_code) after run. 

:return: process output 

""" 

self.print_call_info() 

 

try: 

return subprocess.check_output(self.popenargs, **self.kwargs) 

except subprocess.CalledProcessError as err: 

print("\n%s" % err) 

322 ↛ 323line 322 didn't jump to line 323, because the condition on line 322 was never true if check: 

sys.exit(err.returncode) 

raise 

 

def iter_output(self, check=True): 

""" 

A subprocess with tee ;) 

""" 

self.print_call_info() 

 

orig_timeout = self.kwargs.pop("timeout") 

 

self.kwargs.update({ 

"stdout":subprocess.PIPE, 

"stderr":subprocess.STDOUT, 

}) 

 

proc=subprocess.Popen(self.popenargs, **self.kwargs) 

 

end_time = time.time() + orig_timeout 

for line in iter(proc.stdout.readline, ''): 

yield line 

 

if time.time()>end_time: 

raise subprocess.TimeoutExpired(self.popenargs, orig_timeout) 

 

if check and proc.returncode: 

sys.exit(proc.returncode) 

 

def print_output(self, check=True): 

for line in self.iter_output(check=check): 

print(line, flush=True) 

 

 

def get_pip_file_name(): 

if sys.platform == 'win32': 

return "pip3.exe" 

else: 

return "pip3" 

 

 

 

class DisplayErrors: 

""" 

Decorator to print traceback on exceptions. 

Used in e.g.: Cmd class 

""" 

def __init__(self, func): 

self.func = func 

 

def __call__(self, *args, **kwargs): 

try: 

self.func(*args, **kwargs) 

except Exception as err: 

traceback.print_exc(file=sys.stderr) 

return "%s: %s" % (err.__class__.__name__, err) 

 

 

 

class Cmd2(cmd.Cmd): 

""" 

Enhanced version of 'Cmd' class: 

- command alias 

- methods can be called directly from commandline: e.g.: ./foobar.py --help 

- Display 

""" 

version = __version__ 

 

command_alias = { # used in self.precmd() 

"q": "quit", "EOF": "quit", "exit": "quit", 

"": "help", # Just hit ENTER -> help 

"--help": "help", "-h": "help", "-?": "help", 

} 

 

unknown_command="*** Unknown command: %r ***\n" 

 

# Will be append to 'doc_leader' in self.do_help(): 

complete_hint="\nUse <{key}> to command completion.\n" 

missing_complete="\n(Sorry, no command completion available.)\n" # if 'readline' not available 

 

def __init__(self, *args, self_filename=None, **kwargs): 

super().__init__(*args, **kwargs) 

 

if self_filename is None: 

self.self_filename = SELF_FILE_PATH.name # Path(__file__).name ;) 

else: 

self.self_filename = self_filename 

 

intro_line = '{filename} shell v{version}'.format( 

filename=self.self_filename, 

version=self.version 

) 

intro_line = colorizer.colorize(intro_line, foreground="blue", background="black", opts=("bold",)) 

 

self.intro = ( 

'\n{intro_line}\n' 

'Type help or ? to list commands.\n' 

).format(intro_line=intro_line) 

 

self.prompt = colorizer.colorize(self.self_filename, foreground="cyan") 

self.prompt += colorizer.colorize("> ", opts=("bold",)) 

 

self.doc_header = "Available commands (type help <topic>):\n" 

self.doc_leader = ( 

"\nHint: All commands can be called directly from commandline.\n" 

"e.g.: $ ./{filename} help\n" 

).format( 

filename=self.self_filename, 

) 

 

# e.g.: $ bootstrap_env_admin.py boot /tmp/bootstrap_env-env -> run self.do_boot("/tmp/bootstrap_env-env") on startup 

args = sys.argv[1:] 

434 ↛ exitline 434 didn't return from function '__init__', because the condition on line 434 was never false if args: 

self.cmdqueue = [" ".join(args)] 

 

def default(self, line): 

""" Called on an input line when the command prefix is not recognized. """ 

colorizer.err(self.unknown_command % line, foreground="red") 

 

@DisplayErrors 

def _complete_list(self, items, text, line, begidx, endidx): 

if text: 

return [x for x in items if x.startswith(text)] 

else: 

return items 

 

@DisplayErrors 

def _complete_path(self, text, line, begidx, endidx): 

""" 

complete a command argument with a existing path 

 

usage e.g.: 

class FooCmd(Cmd2): 

def complete_foobar(self, text, line, begidx, endidx): 

return self._complete_path(text, line, begidx, endidx) 

 

def do_foobar(self, path): # 'path' is type string! 

print("path:", path) 

""" 

try: 

destination = line.split(" ", 1)[1] 

except IndexError: 

destination = "." 

 

if destination=="~": 

return [os.sep] 

 

destination = Path(destination).expanduser().resolve() 

 

if not destination.is_dir(): 

destination = destination.parent.resolve() 

 

if destination.is_dir(): 

complete_list = [x.stem + os.sep for x in destination.iterdir() if x.is_dir()] 

if text: 

if text in complete_list: 

return [text + os.sep] 

 

complete_list = [x for x in complete_list if x.startswith(text)] 

else: 

complete_list = [] 

 

return complete_list 

 

def get_doc_line(self, command): 

""" 

return the first line of the DocString. 

If no DocString: return None 

""" 

assert command.startswith("do_") 

doc=getattr(self, command, None).__doc__ 

if doc is not None: 

doc = doc.strip().split("\n",1)[0] 

return doc 

 

_complete_hint_added=False 

def do_help(self, arg): 

""" 

List available commands with "help" or detailed help with "help cmd". 

""" 

502 ↛ 504line 502 didn't jump to line 504, because the condition on line 502 was never true if arg: 

# Help for one command 

return super().do_help(arg) 

 

# List available commands: 

 

self.stdout.write("%s\n" % self.doc_leader) 

self.stdout.write("%s\n" % self.doc_header) 

 

commands = [name for name in self.get_names() if name.startswith("do_")] 

commands.sort() 

max_length = max([len(name) for name in commands]) 

 

for command in commands: 

doc_line = self.get_doc_line(command) or "(Undocumented command)" 

 

command = command[3:] # remove "do_" 

 

command = "{cmd:{width}}".format(cmd=command, width=max_length) 

command = colorizer.colorize(command, opts=("bold",)) 

 

self.stdout.write(" {cmd} - {doc}\n".format( 

cmd=command, 

doc=doc_line 

)) 

 

self.stdout.write("\n") 

 

def do_quit(self, arg): 

"Exit this interactiv shell" 

print("\n\nbye") 

return True 

 

def precmd(self, line): 

""" 

1. Apply alias list 

2. print first DocString line (if exists), before start the command 

""" 

try: 

line=self.command_alias[line] 

except KeyError: 

pass 

 

cmd = line.split(" ",1)[0] 

doc_line = self.get_doc_line("do_%s" % cmd) 

if doc_line: 

colorizer.out("\n\n *** %s ***\n" % doc_line, background="cyan", opts=("bold",)) 

 

return line 

 

def postcmd(self, stop, line): 

# stop if we are called with commandline arguments 

554 ↛ 556line 554 didn't jump to line 556, because the condition on line 554 was never false if len(sys.argv)>1: 

stop = True 

return stop 

 

 

class EnvBuilder(venv.EnvBuilder): 

""" 

* Create new virtualenv 

* install and update pip 

* install "pylucid" 

* call "pylucid_admin.py update_env" to install all requirements 

""" 

verbose = True 

 

def __init__(self, requirements): 

super().__init__(with_pip=True) 

self.requirements = requirements 

 

def create(self, env_dir): 

print(" * Create new pylucid virtualenv here: %r" % env_dir) 

 

if "VIRTUAL_ENV" in os.environ: 

print("\nERROR: Don't call me in a activated virtualenv!") 

print("You are in VIRTUAL_ENV: %r" % os.environ["VIRTUAL_ENV"]) 

return 

 

return super().create(env_dir) 

 

def ensure_directories(self, env_dir): 

print(" * Create the directories for the environment.") 

return super().ensure_directories(env_dir) 

 

def create_configuration(self, context): 

print(" * Create 'pyvenv.cfg' configuration file.") 

return super().create_configuration(context) 

 

def setup_python(self, context): 

print(" * Set up a Python executable in the environment.") 

return super().setup_python(context) 

 

def call_new_python(self, context, *args, check=True, **kwargs): 

""" 

Do the same as bin/activate so that <args> runs in a "activated" virtualenv. 

""" 

kwargs.update({ 

"env_updates": { 

"VIRTUAL_ENV": context.env_dir, 

"PATH": "%s:%s" % (context.bin_path, os.environ["PATH"]), 

} 

}) 

VerboseSubprocess(*args, **kwargs).verbose_call( 

check=check # sys.exit(return_code) if return_code != 0 

) 

 

def _setup_pip(self, context): 

print(" * Install pip in a virtual environment.") 

# install pip with ensurepip: 

super()._setup_pip(context) 

 

print(" * Upgrades pip in a virtual environment.") 

# Upgrade pip first (e.g.: running python 3.5) 

 

context.pip_bin=Path(context.bin_path, get_pip_file_name()) # e.g.: .../bin/pip3 

assert context.pip_bin.is_file(), "Pip not found here: %s" % context.pip_bin 

 

if sys.platform == 'win32': 

# Note: On windows it will crash with a PermissionError: [WinError 32] 

# because pip can't replace himself while running ;) 

# Work-a-round is "python -m pip install --upgrade pip" 

# see also: https://github.com/pypa/pip/issues/3804 

self.call_new_python( 

context, 

context.env_exe, "-m", "pip", "install", "--upgrade", "pip", 

check=False # Don't exit on errors 

) 

else: 

self.call_new_python( 

context, 

str(context.pip_bin), "install", "--upgrade", "pip", 

check=False # Don't exit on errors 

) 

 

def setup_scripts(self, context): 

print(" * Set up scripts into the created environment.") 

return super().setup_scripts(context) 

 

def post_setup(self, context): 

""" 

Set up any packages which need to be pre-installed into the 

virtual environment being created. 

 

:param context: The information for the virtual environment 

creation request being processed. 

""" 

print(" * post-setup modification") 

 

# Install "pylucid" 

# in normal mode as package from PyPi 

# in dev. mode as editable from github 

self.call_new_python( 

context, 

str(context.pip_bin), "install", 

# "--verbose", 

*self.requirements 

) 

 

# Check if ".../bin/pylucid_admin.py" exists 

bootstrap_env_admin_path = Path(context.bin_path, ADMIN_FILE_NAME) 

if not bootstrap_env_admin_path.is_file(): 

print("ERROR: admin script not found here: '%s'" % bootstrap_env_admin_path) 

VerboseSubprocess("ls", "-la", str(context.bin_path)).verbose_call() 

sys.exit(-1) 

 

# Install all requirements by call: "pylucid_admin.py update_env" 

self.call_new_python( 

context, 

context.env_exe, 

str(bootstrap_env_admin_path), 

"update_env", 

timeout=4*60 

) # extended timeout for slow Travis ;) 

 

 

 

class BootBootstrapEnvShell(Cmd2): 

""" 

The bootstrap shell to start the virtualenv creation. 

It's implement only two commands: 

* boot 

* boot_developer 

""" 

def _resolve_path(self, path): 

return Path(path).expanduser().resolve() 

 

def complete_boot(self, text, line, begidx, endidx): 

# print("text: %r" % text) 

# print("line: %r" % line) 

return self._complete_path(text, line, begidx, endidx) 

 

def _parse_requirements(self, requirement_string): 

requirements = [] 

for line in requirement_string.splitlines(): 

line = line.strip() 

if line and not line.startswith("#"): 

 

line = line.split("# ", 1)[0] # Remove pip-compile comments e.g.: "... # via foo" 

line = line.rstrip() 

 

if line.startswith("-e"): # split editables 

requirements += line.split(" ") 

else: 

requirements.append(line) 

return requirements 

 

def _boot(self, destination, requirements): 

""" 

Create a pylucid virtualenv and install requirements. 

""" 

destination = Path(destination).expanduser() 

713 ↛ 717line 713 didn't jump to line 717, because the condition on line 713 was never false if destination.exists(): 

self.stdout.write("\nERROR: Path '%s' already exists!\n" % destination) 

sys.exit(1) 

 

builder = EnvBuilder(requirements) 

builder.create(str(destination)) 

 

self.stdout.write("\n") 

 

if not destination.is_dir(): 

self.stdout.write("ERROR: Creating virtualenv!\n") 

sys.exit(1) 

else: 

self.stdout.write("virtualenv created at: '%s'\n" % destination) 

 

def do_boot(self, destination): 

""" 

Bootstrap pylucid virtualenv in "normal" mode. 

 

usage: 

pylucid_boot> boot [path] 

 

Create a pylucid virtualenv in the given [path]. 

Install packages via PyPi and read-only sources from github. 

 

The destination path must not exist yet! 

 

(used the requirements/normal_installation.txt) 

""" 

self._boot(destination, requirements=NORMAL_INSTALL) 

complete_boot = complete_boot 

 

def do_boot_developer(self, destination): 

""" 

Bootstrap pylucid virtualenv in "developer" mode. 

All own projects installed as editables via github HTTPS (readonly) 

 

**Should be only used for developing/contributing. All others: Use normal 'boot' ;) ** 

 

usage: 

pylucid_boot> boot_developer [path] 

 

Create a pylucid virtualenv in the given [path]. 

Install packages via PyPi and read-only sources from github. 

 

The destination path must not exist yet! 

 

(used the requirements/developer_installation.txt) 

""" 

self._boot(destination, requirements=DEVELOPER_INSTALL) 

complete_boot_developer = complete_boot 

 

 

def main(): 

""" 

Start the shell. 

 

This may also used in setup.py, e.g.: 

entry_points={'console_scripts': [ 

"pylucid_boot = pylucid.pylucid_boot:main", 

]}, 

""" 

BootBootstrapEnvShell().cmdloop() 

 

 

if __name__ == '__main__': 

main()