Skip to content

Commit

Permalink
Windows compatibility (#864)
Browse files Browse the repository at this point in the history
* chore(deps): bump certifi from 2022.9.24 to 2022.12.7 (#861)

Bumps [certifi](https://github.com/certifi/python-certifi) from 2022.9.24 to 2022.12.7.
- [Release notes](https://github.com/certifi/python-certifi/releases)
- [Commits](certifi/python-certifi@2022.09.24...2022.12.07)

---
updated-dependencies:
- dependency-name: certifi
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <[email protected]>

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Added a function to get the current python executable command

* Use .code instead .errno attribute to make OS agnostic

* typo fix

* Use Findstr instead pf grep command if OS == Windows

* Update changelog

* chore(deps): bump activesupport from 6.0.3.6 to 6.0.6.1 in /docs (#865)

Bumps [activesupport](https://github.com/rails/rails) from 6.0.3.6 to 6.0.6.1.
- [Release notes](https://github.com/rails/rails/releases)
- [Changelog](https://github.com/rails/rails/blob/v7.0.4.2/activesupport/CHANGELOG.md)
- [Commits](rails/rails@v6.0.3.6...v6.0.6.1)

---
updated-dependencies:
- dependency-name: activesupport
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* chore(deps): bump nokogiri from 1.13.9 to 1.13.10 in /docs (#860)

Bumps [nokogiri](https://github.com/sparklemotion/nokogiri) from 1.13.9 to 1.13.10.
- [Release notes](https://github.com/sparklemotion/nokogiri/releases)
- [Changelog](https://github.com/sparklemotion/nokogiri/blob/main/CHANGELOG.md)
- [Commits](sparklemotion/nokogiri@v1.13.9...v1.13.10)

---
updated-dependencies:
- dependency-name: nokogiri
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* docs: index.html: add id to donate header

* dependency update

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Robert Zaremba <[email protected]>
  • Loading branch information
3 people committed Jan 31, 2023
1 parent 4e3d54b commit cae1923
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 60 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Features, Bug Fixes, API Breaking, Deprecated, Infrastructure, /example Updates
### Bug Fixes

- Fixed `pre-commit` script that did not work properly and skipped the `lint-staged` part.
- Fixed compiling contracts on Windows.

### Features

Expand Down
38 changes: 15 additions & 23 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions docs/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GEM
remote: https://rubygems.org/
specs:
activesupport (6.0.3.6)
activesupport (6.0.6.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
Expand All @@ -16,7 +16,7 @@ GEM
colorator (1.1.0)
commonmarker (0.17.13)
ruby-enum (~> 0.5)
concurrent-ruby (1.1.8)
concurrent-ruby (1.2.0)
dnsruby (1.61.5)
simpleidn (~> 0.1)
em-websocket (0.5.2)
Expand Down Expand Up @@ -211,17 +211,17 @@ GEM
jekyll (>= 3.5, < 5.0)
jekyll-feed (~> 0.9)
jekyll-seo-tag (~> 2.1)
minitest (5.14.4)
minitest (5.17.0)
multipart-post (2.1.1)
nokogiri (1.13.9-x86_64-linux)
nokogiri (1.13.10-x86_64-linux)
racc (~> 1.4)
octokit (4.20.0)
faraday (>= 0.9)
sawyer (~> 0.8.0, >= 0.5.3)
pathutil (0.16.2)
forwardable-extended (~> 2.6)
public_suffix (4.0.6)
racc (1.6.0)
racc (1.6.1)
rb-fsevent (0.10.4)
rb-inotify (0.10.1)
ffi (~> 1.0)
Expand Down Expand Up @@ -253,7 +253,7 @@ GEM
unf_ext
unf_ext (0.0.7.7)
unicode-display_width (1.7.0)
zeitwerk (2.4.2)
zeitwerk (2.6.6)

PLATFORMS
x86_64-linux
Expand Down
3 changes: 2 additions & 1 deletion packages/algob/src/lib/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import YAML from "yaml";
import { assertDir, ASSETS_DIR, CACHE_DIR } from "../internal/core/project-structure";
import { timestampNow } from "../lib/time";
import type { ASCCache, SCParams } from "../types";
import { NO_FILE_OR_DIRECTORY_ERROR } from "./constants";

export const tealExt = ".teal";
export const pyExt = ".py";
Expand Down Expand Up @@ -111,7 +112,7 @@ export class CompileOp {
const p = path.join(CACHE_DIR, filename + ".yaml");
return YAML.parse(await fs.promises.readFile(p, "utf8")) as ASCCache;
} catch (e) {
if (types.isFileError(e) && e?.errno === -2) {
if ((e as NodeJS.ErrnoException)?.code === NO_FILE_OR_DIRECTORY_ERROR) {
return undefined;
} // handling a not existing file
throw e;
Expand Down
1 change: 1 addition & 0 deletions packages/algob/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export const MIN_UINT64 = 0n;
export const MAX_UINT64 = 0xffffffffffffffffn;
// mock algod credentials
export const mockAlgod = new Algodv2("dummyToken", "https://dummyNetwork", 8080);
export const NO_FILE_OR_DIRECTORY_ERROR = "ENOENT"
5 changes: 3 additions & 2 deletions packages/algob/src/lib/files.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { getPathFromDirRecursive } from "@algo-builder/runtime";
import { BuilderError, ERRORS, types } from "@algo-builder/web";
import { BuilderError, ERRORS } from "@algo-builder/web";
import fs from "fs-extra";
import path from "path";

import { ASSETS_DIR } from "../internal/core/project-structure";
import { NO_FILE_OR_DIRECTORY_ERROR } from "./constants";

function normalizePaths(mainPath: string, paths: string[]): string[] {
return paths.map((n) => path.relative(mainPath, n));
Expand Down Expand Up @@ -45,7 +46,7 @@ export function loadEncodedTxFromFile(fileName: string): Uint8Array | undefined
const buffer = fs.readFileSync(p);
return Uint8Array.from(buffer);
} catch (e) {
if (types.isFileError(e) && e?.errno === -2) {
if ((e as NodeJS.ErrnoException)?.code === NO_FILE_OR_DIRECTORY_ERROR) {
return undefined;
} // handling a not existing file
throw e;
Expand Down
6 changes: 3 additions & 3 deletions packages/algob/src/lib/msig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getPathFromDirRecursive } from "@algo-builder/runtime";
import { types } from "@algo-builder/web";
import {
Account,
appendSignMultisigTransaction,
Expand All @@ -17,6 +16,7 @@ import fs from "fs";

import { ASSETS_DIR } from "../internal/core/project-structure";
import { LogicSig } from "../types";
import { NO_FILE_OR_DIRECTORY_ERROR } from "./constants";
import { isSignedTx } from "./tx";

export const blsigExt = ".blsig";
Expand Down Expand Up @@ -56,7 +56,7 @@ export async function readMsigFromFile(filename: string): Promise<EncodedMultisi
const msig = fs.readFileSync(p, "utf8").split("LogicSig: ")[1];
return await decodeMsigObj(msig);
} catch (e) {
if (types.isFileError(e) && e?.errno === -2) {
if ((e as NodeJS.ErrnoException)?.code === NO_FILE_OR_DIRECTORY_ERROR) {
return undefined;
} // handling a not existing file
throw e;
Expand All @@ -77,7 +77,7 @@ export async function readBinaryMultiSig(filename: string): Promise<string | und
const p = getPathFromDirRecursive(ASSETS_DIR, filename) as string;
return fs.readFileSync(p, "base64");
} catch (e) {
if (types.isFileError(e) && e?.errno === -2) {
if ((e as NodeJS.ErrnoException)?.code === NO_FILE_OR_DIRECTORY_ERROR) {
return undefined;
} // handling a not existing file
throw e;
Expand Down
8 changes: 8 additions & 0 deletions packages/runtime/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -888,3 +888,11 @@ export enum NumIndex {
export const JS_CONFIG_FILENAME = "algob.config.js";
export const TS_CONFIG_FILENAME = "algob.config.ts";
export const NETWORK_DEFAULT = "default";

export const PythonCommands = ['python3', 'python', 'py'] as const;
export type PythonCommand = typeof PythonCommands[number];

export enum searchStrCommand {
Windows = "Findstr",
UnixLinux = "grep",
}
44 changes: 40 additions & 4 deletions packages/runtime/src/lib/pycompile-op.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { BuilderError, ERRORS } from "@algo-builder/web";
import { exec, spawnSync, SpawnSyncReturns } from "child_process";
import { exec, execSync, spawnSync, SpawnSyncReturns } from "child_process";
import YAML from "yaml";

import type { ReplaceParams, SCParams } from "../types";
import { PythonCommand, PythonCommands, searchStrCommand } from "./constants";
import { getPathFromDirRecursive } from "./files";
import Os from "os";

export const tealExt = ".teal";
export const pyExt = ".py";
Expand Down Expand Up @@ -34,6 +36,20 @@ export class PyCompileOp {
return content;
}

/**
* Description: Check if current OS is Windows
*/
private isWindows(): boolean {
return Os.platform() === "win32";
}

/**
* Description: Returns the command to search strings according to OS
*/
private getSearchStrCommand(): searchStrCommand {
return this.isWindows() ? searchStrCommand.Windows : searchStrCommand.UnixLinux;
}

/**
* Parses scTmplParams and returns ReplaceParams and stringify object
* @param scTmplParams smart contract template parameters
Expand Down Expand Up @@ -77,27 +93,47 @@ export class PyCompileOp {
return program;
}

/**
* Description: Check and returns the executable python command
*/
private getPythonCommand(): PythonCommand {
const pyCommand = PythonCommands.find((command: PythonCommand) => {
try {
execSync(command + " -V", { stdio: "pipe" });
} catch {
return false;
}
return command;
});
if (!pyCommand) {
throw new Error(`executable python command not found.`);
}
return pyCommand;
}

/**
* Description: Runs a subprocess to execute python script
* @param filename : python filename in assets folder
* @param scInitParam : Smart contract initialization parameters.
*/
private runPythonScript(filename: string, scInitParam?: string): SpawnSyncReturns<string> {
const filePath = getPathFromDirRecursive(ASSETS_DIR, filename) as string;
const pythonCommand = this.getPythonCommand();
// used spawnSync instead of spawn, as it is synchronous
if (scInitParam === undefined) {
return spawnSync("python3", [filePath], { encoding: "utf8" });
return spawnSync(pythonCommand, [filePath], { encoding: "utf8" });
}

return spawnSync("python3", [filePath, scInitParam], { encoding: "utf8" });
return spawnSync(pythonCommand, [filePath, scInitParam], { encoding: "utf8" });
}

/**
* Description: This method checks if given module is installed or not. Otherwise throw an exception.
* @param module: Module to be checked if installed or not.
*/
private validatePythonModule(module: string) {
exec(`pip list | grep ${module}`, (err: any) => {
const searchStrCommand = this.getSearchStrCommand();
exec(`pip list | ${searchStrCommand} ${module}`, (err: any) => {
if (err) {
throw new Error(
`"${module}" module not found. Please try running "pip install ${module}"`
Expand Down
9 changes: 0 additions & 9 deletions packages/web/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,15 +334,6 @@ export interface RequestError extends Error {
error?: Error;
}

export interface FileError extends Error {
errno: number;
}

// This function is used to check if given objects implements `FileError` interface
export function isFileError(object: unknown): object is FileError {
return Object.prototype.hasOwnProperty.call(object, "errno");
}

// This function is used to check if given objects implements `RequestError` interface
// https://www.technicalfeeder.com/2021/02/how-to-check-if-a-object-implements-an-interface-in-typescript/
export function isRequestError(object: unknown): object is RequestError {
Expand Down
15 changes: 3 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4291,13 +4291,13 @@ __metadata:
linkType: hard

"json5@npm:^1.0.1":
version: 1.0.1
resolution: "json5@npm:1.0.1"
version: 1.0.2
resolution: "json5@npm:1.0.2"
dependencies:
minimist: ^1.2.0
bin:
json5: lib/cli.js
checksum: e76ea23dbb8fc1348c143da628134a98adf4c5a4e8ea2adaa74a80c455fc2cdf0e2e13e6398ef819bfe92306b610ebb2002668ed9fc1af386d593691ef346fc3
checksum: 866458a8c58a95a49bef3adba929c625e82532bcff1fe93f01d29cb02cac7c3fe1f4b79951b7792c2da9de0b32871a8401a6e3c5b36778ad852bf5b8a61165d7
languageName: node
linkType: hard

Expand Down Expand Up @@ -5535,15 +5535,6 @@ __metadata:
languageName: node
linkType: hard

"qs@npm:^6.9.4":
version: 6.11.0
resolution: "qs@npm:6.11.0"
dependencies:
side-channel: ^1.0.4
checksum: 6e1f29dd5385f7488ec74ac7b6c92f4d09a90408882d0c208414a34dd33badc1a621019d4c799a3df15ab9b1d0292f97c1dd71dc7c045e69f81a8064e5af7297
languageName: node
linkType: hard

"query-string@npm:6.13.5":
version: 6.13.5
resolution: "query-string@npm:6.13.5"
Expand Down

0 comments on commit cae1923

Please sign in to comment.